Javascript

JavaScript (js) is a light-weight object-oriented programming language which is used by several websites for scripting the webpages.

 

CategoryFunction NameDescriptionExample
Global Functionseval()Executes a string of JavaScript code.eval('2 + 2')
 isNaN()Checks if a value is NaN (Not-a-Number).isNaN('hello') // true
 isFinite()Determines whether a value is a finite number.isFinite(123)
 parseInt()Parses a string and returns an integer.parseInt('123') // 123
 parseFloat()Parses a string and returns a floating-point number.parseFloat('123.45') // 123.45
 decodeURIComponent()Decodes a URI component that was previously encoded.decodeURIComponent('Hello%20World')
 encodeURIComponent()Encodes a URI component (e.g., an individual query parameter).encodeURIComponent('Hello World')
 decodeURI()Decodes a full URI string.decodeURI('https://www.example.com/?name=John%20Doe')
 encodeURI()Encodes a URI string (the whole URL).encodeURI('https://www.example.com/?name=John Doe')
Math FunctionsMath.abs()Returns the absolute value of a number.Math.abs(-5) // 5
 Math.ceil()Rounds a number UP to the nearest integer.Math.ceil(4.3) // 5
 Math.floor()Rounds a number DOWN to the nearest integer.Math.floor(4.7) // 4
 Math.round()Rounds a number to the nearest integer.Math.round(4.4) // 4
 Math.random()Returns a random number between 0 (inclusive) and 1 (exclusive).Math.random()
 Math.max()Returns the largest of the numbers provided.Math.max(2, 5, 10) // 10
 Math.min()Returns the smallest of the numbers provided.Math.min(2, 5, 10) // 2
 Math.pow()Returns the base to the exponent power.Math.pow(2, 3) // 8
 Math.sqrt()Returns the square root of a number.Math.sqrt(9) // 3
 Math.sin()Returns the sine of a number (in radians).Math.sin(Math.PI / 2) // 1
 Math.cos()Returns the cosine of a number (in radians).Math.cos(Math.PI) // -1
 Math.tan()Returns the tangent of a number (in radians).Math.tan(Math.PI / 4) // 1
Array FunctionsArray.isArray()Checks if a value is an array.Array.isArray([1, 2, 3]) // true
 Array.prototype.push()Adds one or more elements to the end of an array.[1, 2].push(3) // [1, 2, 3]
 Array.prototype.pop()Removes the last element from an array.[1, 2, 3].pop() // 3
 Array.prototype.shift()Removes the first element from an array.[1, 2, 3].shift() // 1
 Array.prototype.unshift()Adds one or more elements to the beginning of an array.[1, 2].unshift(0) // [0, 1, 2]
 Array.prototype.concat()Combines multiple arrays into one.[1].concat([2, 3]) // [1, 2, 3]
 Array.prototype.join()Joins all elements of an array into a single string.[1, 2, 3].join('-') // "1-2-3"
 Array.prototype.slice()Returns a shallow copy of a portion of an array.[1, 2, 3].slice(1) // [2, 3]
 Array.prototype.splice()Adds/removes items from an array at a specific index.[1, 2, 3].splice(1, 1) // [2]
 Array.prototype.sort()Sorts the elements of an array.[3, 1, 2].sort() // [1, 2, 3]
 Array.prototype.filter()Creates a new array with all elements that pass the test in the provided function.[1, 2, 3].filter(x => x > 1) // [2, 3]
 Array.prototype.map()Creates a new array with the results of calling a provided function on every element in the array.[1, 2, 3].map(x => x * 2) // [2, 4, 6]
String FunctionsString.prototype.charAt()Returns the character at the specified index.'Hello'.charAt(1) // 'e'
 String.prototype.concat()Joins two or more strings and returns a new string.'Hello'.concat(' World') // 'Hello World'
 String.prototype.includes()Checks if a string contains the specified substring.'Hello'.includes('ell') // true
 String.prototype.indexOf()Returns the index of the first occurrence of a specified value in a string.'Hello'.indexOf('e') // 1
 String.prototype.slice()Extracts a section of a string and returns a new string.'Hello'.slice(1, 3) // 'el'
 String.prototype.split()Splits a string into an array of substrings.'Hello World'.split(' ') // ['Hello', 'World']
 String.prototype.toLowerCase()Converts a string to lowercase.'Hello'.toLowerCase() // 'hello'
 String.prototype.toUpperCase()Converts a string to uppercase.'Hello'.toUpperCase() // 'HELLO'
Object FunctionsObject.assign()Copies all enumerable properties from one or more source objects to a target object.Object.assign({}, {a: 1}, {b: 2}) // {a: 1, b: 2}
 Object.keys()Returns an array of the enumerable property names of an object.Object.keys({a: 1, b: 2}) // ['a', 'b']
 Object.values()Returns an array of the enumerable property values of an object.Object.values({a: 1, b: 2}) // [1, 2]
 Object.entries()Returns an array of key-value pairs of an object.Object.entries({a: 1, b: 2}) // [['a', 1], ['b', 2]]
 Object.freeze()Freezes an object, making it immutable.Object.freeze({a: 1}) // {a: 1}
 Object.hasOwnProperty()Checks if an object has a specified property as its own (not inherited).{a: 1}.hasOwnProperty('a') // true
JSON FunctionsJSON.parse()Converts a JSON string into a JavaScript object.JSON.parse('{"a": 1}') // {a: 1}
 JSON.stringify()Converts a JavaScript object into a JSON string.JSON.stringify({a: 1}) // '{"a":1}'
DOM Functionsdocument.getElementById()Returns the element with the specified ID.document.getElementById('myId')
 document.querySelector()Returns the first element that matches a specified CSS selector.document.querySelector('.myClass')
 document.createElement()Creates a new HTML element.document.createElement('div')
 element.appendChild()Adds a node as the last child of an element.element.appendChild(newNode)
 element.addEventListener()Adds an event listener to an element.element.addEventListener('click', function() {})