Categories
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.

Errors

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

try {
  undefinedFunction();
} catch (err) {
  console.log(err.message);
}

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

try {
  undefinedFunction();
} catch (err) {
  console.log(err.message);
} finally {
  console.log('done');
}

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;

NaN

We can check for NaN values with isNaN :

isNaN(x)

Run Code After a Delay

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

setTimeout(() => {

}, 1000);

Functions

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 :

console.log(a);

We can also show an alert box with alert :

alert(a);

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");

Comments

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

// One line

And we can add a multiline comment with:

/* Multi line
comment */

Strings

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 :

abc.indexOf("abc");

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

abc.lastIndexOf("de");

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:

abc.replace("abc","123");

We can convert a string to upper case with toUpperCase :

abc.toUpperCase();

We can convert a string to upper case with toLowerCase :

abc.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 [] :

abc.charAt(2);
abc[2];

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

abc.charCodeAt(2);

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

abc.split(",");

We can split a string by an empty string:

abc.split("");

to split a string into an array of characters.

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

128.toString(16);

Conclusion

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 *