JavaScript Basics

JavaScript Cheat Sheet — Errors and Strings

Spread the love

JavaScript is one of the most popular programming languages for web programming.

In this article, we’ll look at the basic syntax of modern JavaScript.


We can use the try-catch block to catch errors from code that may raise errors:

try {
} catch (err) {

We can run code regardless of whether an error is thrown with the finally block:

try {
} catch (err) {
} finally {

We can throw errors by writing:

throw new Error('error')

JavaScript comes with various kinds of error classes:

  • RangeError — A number is out of range
  • ReferenceError — An illegal reference has occurred
  • SyntaxError — A syntax error has occurred
  • TypeError — A type error has occurred
  • URIError — An encodeURI() error has occurred

Input Values

We can get the entered value from an input element with the value property:

const val = document.querySelector("input").value;


We can check for NaN values with isNaN :


Run Code After a Delay

We can run code after a delay with the setTimeout function:

setTimeout(() => {

}, 1000);


We can declare functions with the function keyword:

function addNumbers(a, b) {
  return a + b;;

Update DOM Element Content

We can update DOM element content by setting the innerHTML property:

document.getElementById("elementID").innerHTML = "Hello World";

Output Data

To log data to the console, we call console.log :


We can also show an alert box with alert :


Also, we can show a confirm dialog box by calling confirm :

confirm("Are you sure?");

We can ask the user for inputs with the prompt function:

prompt("What's your age?", "0");


We can add comments to our JavaScript code with // :

// One line

And we can add a multiline comment with:

/* Multi line
comment */


We can declare strings with quotes:

let abc = "abcde";

Also, we can add a new line character with n :

let esc = 'I don't n know';

We get the length of a string with the length property:

let len = abc.length;

We get the index of a substring in a given string with indexOf :


Also, we can get the last occurrence of a substring in a string with lastIndexOf :


We can get a substring between the given indexes with the slice method:

abc.slice(3, 6);

The replace method lets us replace a substring with another substring:


We can convert a string to upper case with toUpperCase :


We can convert a string to upper case with toLowerCase :


We can combine one string with another with concat :

abc.concat(" ", str2);

And we can get the character at the given index with charAt or [] :


The charCodeAt method lets us get the character code at the given index:


The split method lets us split a string by the given separator:


We can split a string by an empty string:


to split a string into an array of characters.

And we can convert a number to a string with the given base with toString :



We can throw and catch errors with JavaScript.

And we can use various methods to work with strings.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *