JavaScript Cheat Sheet: Essential Concepts for Beginners

This cheat sheet covers the most essential JavaScript concepts that beginners need to know, including variables, data types, operators, functions, arrays, and conditional statements.

Whether you're just starting out with JavaScript or you need a quick refresher, this cheat sheet is the perfect resource for you.

📘 15+ Best JavaScript Books for Beginners and Experienced Coders

JavaScript CheatSheet for Developers

JavaScript Basics

JavaScript Code in the HTML Page

<script type="text/javascript">
    // JS code goes here
</script>

Comments

// Single line comments

/* Multi-line comments */ 

Variables

VariablesDescription
varThe most common variable. Can be reassigned but only accessed within a function, Variables defined with var move to the top when code is executed.
constCannot be reassigned and not accessible before they appear within the code.
letSimilar to const, however, let variable can be reassigned but not re-declared.

Javascript String Methods Cheat Sheet: The Complete Guide to Javascript String Methods

Arrays

// Example
var names= ["Raj", "Ram", "Sham"];

Array Methods

MethodDescription
push()Adds an element to the end of an array
pop()Removes the last element of an array
shift()Removes the first element of an array
unshift()Adds an element to the beginning of an array
slice()Selects a part of an array and returns the new array
splice()Adds/Removes elements to/from an array
concat()Joins two or more arrays, and returns a copy of the joined arrays
indexOf()Search the array for an element and returns its position
lastIndexOf()Search the array for an element, starting at the end, and returns its position
join()Joins all elements of an array into a string
reverse()Reverses the order of the elements in an array
sort()Sorts the elements of an array
forEach()Calls a function for each array element
map()Creates a new array with the result of calling a function for each array element
filter()Creates a new array with every element in an array that pass a test
reduce()Reduce the values of an array to a single value (going left-to-right)
reduceRight()Reduce the values of an array to a single value (going right-to-left)
every()Check if all array values pass a test
some()Check if some array values pass a test
find()Returns the value of the first array element that pass a test
findIndex()Returns the index of the first array element that pass a test
fill()Fill the elements in an array with a static value
copyWithin()Copy array elements within the array
includes()Check if an array contains the specified element
flat()Creates a new array with sub-array elements concatenated into it
flatMap()First maps each element using a mapping function, then flattens the result into a new array
keys()Returns a Array Iterator object with the keys of an array
values()Returns a Array Iterator object with the values of an array
entries()Returns a Array Iterator object with key/value pairs
at()Returns the element at the specified index in an array

Vanilla JavaScript Cheat Sheet: Everything You Need to Know About Vanilla JavaScript

Array.Prototype.Push()

// Syntax
array.push(element1, element2, ..., elementN);
// Example
var names= ["Raj", "Ram", "Sham"];
names.push("Ramesh");
// Output
["Raj", "Ram", "Sham", "Ramesh"]

Array.Prototype.Pop()

// Syntax
array.pop();
// Example
var names= ["Raj", "Ram", "Sham"];
names.pop();
// Output
["Raj", "Ram"]

Array.Prototype.Shift()

 

JavaScript Array Methods Cheat Sheet for Developers

// Syntax
array.shift();
// Example
var names= ["Raj", "Ram", "Sham"];
names.shift();
// Output
["Ram", "Sham"]

Array.Prototype.Unshift()

// Syntax
array.unshift(element1, element2, ..., elementN);
// Example
var names= ["Raj", "Ram", "Sham"];
names.unshift("Ramesh");
// Output
["Ramesh", "Raj", "Ram", "Sham"]

Array.Prototype.Slice()

// Syntax
array.slice();
array.slice(start);
array.slice(start, end);
// Example
var names= ["Raj", "Ram", "Sham"];
names.slice(1, 2);
// Output
["Ram"]

Array.Prototype.Splice()

Modern JavaScript Cheat Sheet for Developers

// Syntax
array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1)
array.splice(start, deleteCount, item1, item2, itemN)
// Example
var names= ["Raj", "Ram", "Sham"];
names.splice(1, 2);
// Output
["Ram", "Sham"]

Array.Prototype.Concat()

// Syntax
array.concat(array1, array2, ..., arrayN);
// Example
var names= ["Raj", "Ram", "Sham"];
var names2= ["Ramesh", "Rajesh", "Rakesh"];
names.concat(names2);
// Output
["Raj", "Ram", "Sham", "Ramesh", "Rajesh", "Rakesh"]

Array.Prototype.IndexOf()

// Syntax
array.indexOf(searchElement);
// Example
var names= ["Raj", "Ram", "Sham"];
names.indexOf("Ram");
// Output
1

Array.Prototype.LastIndexOf()

// Syntax
array.lastIndexOf(searchElement);
// Example
var names= ["Raj", "Ram", "Sham", "Ram"];
names.lastIndexOf("Ram");
// Output
3

Array.Prototype.Join()

// Syntax
array.join(separator);
// Example
var names= ["Raj", "Ram", "Sham"];
names.join(" ");
// Output
Raj Ram Sham

Array.Prototype.Reverse()

// Syntax
array.reverse();
// Example
var names= ["Raj", "Ram", "Sham"];
names.reverse();
// Output
["Sham", "Ram", "Raj"]

Array.Prototype.Sort()

// Syntax
array.sort();
array.sort(compareFunction);
// Example
var names= ["Raj", "Ram", "Sham"];
names.sort();
// Output
["Ram", "Raj", "Sham"]

Array.Prototype.ForEach()

// Syntax
array.forEach(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.forEach(function(name) {
    console.log(name);
});
// Output
Raj
Ram
Sham

Array.Prototype.Map()

// Syntax
array.map(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.map(function(name) {
    return name + " Singh";
});
// Output
["Raj Singh", "Ram Singh", "Sham Singh"]

Array.Prototype.Filter()

// Syntax
array.filter(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.filter(function(name) {
    return name.length > 3;
});
// Output
["Raj", "Sham"]

Array.Prototype.Reduce()

// Syntax
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.reduce(function(total, name) {
    return total + name;
}, "");
// Output
RajRamSham

// Example
var numbers= [1, 2, 3];
numbers.reduce(function(total, number) {
    return total * number;
}, 1);
// Output
6

Array.Prototype.ReduceRight()

// Syntax
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.reduceRight(function(total, name) {
    return total + name;
}, "");
// Output
ShamRamRaj

Array.Prototype.Every()

// Syntax
array.every(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.every(function(name) {
    return name.length > 3;
});
// Output
false

Array.Prototype.Some()

// Syntax
array.some(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.some(function(name) {
    return name.length > 3;
});
// Output
true

Array.Prototype.Find()

// Syntax
array.find(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.find(function(name) {
    return name.length > 3;
});
// Output
Raj

Array.Prototype.FindIndex()

// Syntax
array.findIndex(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.findIndex(function(name) {
    return name.length > 3;
});
// Output
0

Array.Prototype.Fill()

// Syntax
array.fill(value, start, end)
// Example
var names= ["Raj", "Ram", "Sham"];
names.fill("Ramesh");
// Output
["Ramesh", "Ramesh", "Ramesh"]

// Example
var names= ["Raj", "Ram", "Sham"];
names.fill("Ramesh", 1, 2);
// Output
["Raj", "Ramesh", "Sham"]

Array.Prototype.CopyWithin()

// Syntax
array.copyWithin(target, start, end)
// Example
var names= ["Raj", "Ram", "Sham"];
names.copyWithin(1, 0);
// Output
["Raj", "Raj", "Ram"]

// Example
var names= ["Raj", "Ram", "Sham"];
names.copyWithin(1, 0, 2);
// Output
["Raj", "Raj", "Sham"]

Array.Prototype.Includes()

// Syntax
array.includes(searchElement, fromIndex)
// Example
var names= ["Raj", "Ram", "Sham"];
names.includes("Ram");
// Output
true

// Example
var names= ["Raj", "Ram", "Sham"];
names.includes("Ram", 1);
// Output
false

Array.Prototype.Flat()

// Syntax
array.flat(depth)
// Example
var names= ["Raj", ["Ram", "Sham"]];
names.flat();
// Output
["Raj", "Ram", "Sham"]

// Example
var names= ["Raj", ["Ram", ["Sham"]]];
names.flat(1);
// Output
["Raj", "Ram", ["Sham"]]

Array.Prototype.FlatMap()

// Syntax
array.flatMap(function(currentValue, index, arr), thisValue)
// Example
var names= ["Raj", "Ram", "Sham"];
names.flatMap(function(name) {
    return name + " Singh";
});
// Output
["Raj Singh", "Ram Singh", "Sham Singh"]

Array.Prototype.Keys()

// Syntax
array.keys()
// Example
var names= ["Raj", "Ram", "Sham"];
var iterator = names.keys();
iterator.next();
// Output
{value: 0, done: false}

Array.Prototype.Values()

// Syntax
array.values()
// Example
var names= ["Raj", "Ram", "Sham"];
var iterator = names.values();
iterator.next();
// Output
{value: "Raj", done: false}

Array.Prototype.Entries()

// Syntax
array.entries()
// Example
var names= ["Raj", "Ram", "Sham"];
var iterator = names.entries();
iterator.next();
// Output
{value: Array(2), done: false}

Array.Prototype.at()

// Syntax
array.at(index)
// Example
var names= ["Raj", "Ram", "Sham"];
names.at(1);
// Output
Ram

Functions

function name(parameter1, parameter2, parameter3) {
    // what the function does
}

Loops

for (Initialization; Condition; Increment/Decrement) {
    // what to do during the loop
}

For

The most common way to create a loop in Javascript

While

Sets up conditions under which a loop executes

Do While

Similar to the while loop, however, it executes at least once and performs a check at the end to see if the condition is met to execute again

Break

Used to stop and exit the cycle at certain conditions continue Skip parts of the cycle if certain conditions are met

Strings

var event = "Hacktoberfest 2022";

String Methods

MethodDescription
charAt()Returns a character at a specified position inside a string
charCodeAt()Gives you the unicode of character at that position
concat()Concatenates (joins) two or more strings into one
fromCharCode()Returns a string created from the specified sequence of UTF-16 code units
indexOf()Provides the position of the first occurrence of a specified text within a string
lastIndexOf()Same as indexOf() but with the last occurrence, searching backwards
match()Retrieves the matches of a string against a search pattern
replace()Find and replace specific text in a string
search()Executes a search for a matching text and returns its position
slice()Extracts a section of a string and returns it as a new string
split()Splits a string object into an array of strings at a specified position
substr()Similar to slice() but extracts a substring depended on a specified number of characters
substring()Also similar to slice() but can’t accept negative indices
toLowerCase()Convert strings to lowercase
toUpperCase()Convert strings to uppercase
valueOf()Returns the primitive value (that has no properties or methods) of a string object

Numbers and Math

Number Properties

MethodDescription
MAX_VALUEThe maximum numeric value representable in JavaScript
MIN_VALUESmallest positive numeric value representable in JavaScript
NaNThe “Not-a-Number” value
NEGATIVE_INFINITYThe negative Infinity value
POSITIVE_INFINITYPositive Infinity value

Dealing with Dates

Contents need to be added in this section.

Setting Dates

MethodDescription
Date()Creates a new date object with the current date and time
Date(2022, 5, 21, 3, 23, 10, 0)Create a custom date object. The numbers represent year, month, day, hour, minutes, seconds, milliseconds. You can omit anything you want except for year and month.
Date("2017-06-23")Date declaration as a string

Pulling Date and Time Values

FunctionDescription
getDate()Get the day of the month as a number (1-31)
getDay()The weekday as a number (0-6)
getFullYear()Year as a four digit number (yyyy)
getHours()Get the hour (0-23)
getMilliseconds()The millisecond (0-999)
getMinutes()Get the minute (0-59)
getMonth()Month as a number (0-11)
getSeconds()Get the second (0-59)
getTime()Get the milliseconds since January 1, 1970
getUTCDate()The day (date) of the month in the specified date according to universal time (also available for day, month, fullyear, hours, minutes etc.)

Setting Part of a Date

MethodDescription
setDate()Set the day as a number (1-31)
setFullYear()Sets the year (optionally month and day)
setHours()Set the hour (0-23)
setMilliseconds()Set milliseconds (0-999)
setMinutes()Sets the minutes (0-59)
setMonth()Set the month (0-11)
setSeconds()Sets the seconds (0-59)
setTime()Set the time (milliseconds since January 1, 1970)
setUTCDate()Sets the day of the month for a specified date according to universal time (also available for day, month, fullyear, hours, minutes etc.)

Format Date Object

MethodDescription
toLocaleDateString()Returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone

 

DOM (Document Object Modulation)

Element Methods

MethodDescription
getAttribute()Returns the specified attribute value of an element node
getAttributeNS()Returns string value of the attribute with the specified namespace and name
getAttributeNode()Gets the specified attribute node
getAttributeNodeNS()Returns the attribute node for the attribute with the given namespace and name
getElementsByTagName()Provides a collection of all child elements with the specified tag name
getElementsByTagNameNS()Returns a live HTMLCollection of elements with a certain tag name belonging to the given namespace
hasAttribute()Returns true if an element has any attributes, otherwise false
hasAttributeNS()Provides a true/false value indicating whether the current element in a given namespace has the specified attribute
removeAttribute()Removes a specified attribute from an element
removeAttributeNS()Removes the specified attribute from an element within a certain namespace
removeAttributeNode()Takes away a specified attribute node and returns the removed node
setAttribute()Sets or changes the specified attribute to a specified value
setAttributeNS()Adds a new attribute or changes the value of an attribute with the given namespace and name
setAttributeNode()Sets or changes the specified attribute node
setAttributeNodeNS()Adds a new name spaced attribute node to an element

Events

Mouse

MethodDescription
onclickThe event occurs when the user clicks on an element
oncontextmenuUser right-clicks on an element to open a context menu
ondblclickThe user double-clicks on an element
onmousedownUser presses a mouse button over an element
onmouseenterThe pointer moves onto an element
onmouseleavePointer moves out of an element
onmousemoveThe pointer is moving while it is over an element
onmouseoverWhen the pointer is moved onto an element or one of its children
onmouseoutUser moves the mouse pointer out of an element or one of its children
onmouseupThe user releases a mouse button while over an element

Keyboard

MethodDescription
onkeydownWhen the user is pressing a key down
onkeypressThe moment the user starts pressing a key
onkeyupThe user releases a key

Frame

MethodDescription
onabortThe loading of a media is aborted
onbeforeunloadEvent occurs before the document is about to be unloaded
onerrorAn error occurs while loading an external file
onhashchangeThere have been changes to the anchor part of a URL onload When an object has loaded
onpagehideThe user navigates away from a webpage
onpageshowWhen the user navigates to a webpage
onresizeThe document view is resized
onscrollAn element’s scrollbar is being scrolled
onunloadEvent occurs when a page has unloaded

Form

MethodDescription
onblurWhen an element loses focus
onchangeThe content of a form element changes
onfocusAn element gets focus
onfocusinWhen an element is about to get focus
onfocusoutThe element is about to lose focus
oninputUser input on an element
oninvalidAn element is invalid
onresetA form is reset
onsearchThe user writes something in a search field

Errors

Error HandlersDescription
tryLets you define a block of code to test for errors
catchSet up a block of code to execute in case of an error
throwCreate custom error messages instead of the standard JavaScript errors
finallyLets you execute code, after try and catch, regardless of the result

#javascript #js

JavaScript Cheat Sheet: Essential Concepts for Beginners
10.25 GEEK