JavaScript Cheat Sheet

Hover over a Property or Method for an example and click to go to the MDN documentation

Hover over a Property or Method for an example and click to go to the MDN documentation

The length property represents the length of a string.

var example = 'This is an example';

console.log(example 'has ' + example.length + ' characters.');

Output:
This is an example has 18 characters.

The charAt( ) method returns the specified character from a string.

var anyString = 'Brave new world';

console.log("The character at index 0 is '" + anyString.charAt(0) + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");

Output:
The character at index 0 is 'B'
The character at index 999 is ''

The concat( ) method combines the text of two or more strings and returns a new string.

var hello = 'Hello, ';

console.log(hello.concat('Kevin', ' have a nice day'));

Output:
Hello, Kevin have a nice day.

The includes( ) method determines whether one string may be found within another string, returning true or false as appropriate.

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));
console.log(str.includes('nonexistent'));

Output:
True
False

The slice( ) method extracts a section of a string and returns a new string.

var str = 'The morning is upon us.';

console.log(str.slice(4, -2));
console.log(str.slice(0, -1));

Output:
morning is upon u
The morning is upon us

The split( ) method splits a String object into an array of strings by separating the string into substrings.

var myString = 'Hello World!';

console.log(myString.split(''));

Output:
['H','e','l','l','o',' ','W','o','r','l','d','!']

Reversing a String using split( )

console.log(myString.split('').reverse().join(''));

Output:
!dlroW olleH