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
| Variables | Description |
|---|---|
var | The most common variable. Can be reassigned but only accessed within a function, Variables defined with var move to the top when code is executed. |
const | Cannot be reassigned and not accessible before they appear within the code. |
let | Similar to const, however, let variable can be reassigned but not re-declared. |
Arrays
// Example
var names= ["Raj", "Ram", "Sham"];
Array Methods
| Method | Description |
|---|---|
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 |
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()
// 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()
// 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"]