JavaScript

All JavaScript Array Method here…

push(): adds one or more elements to the end of an array.

Example:

let fruits = ["apple", "banana"];
fruits.push("orange");
console.log(fruits); // ["apple", "banana", "orange"]

pop(): removes the last element from an array.

Example:

let fruits = ["apple", "banana", "orange"];
fruits.pop();
console.log(fruits); // ["apple", "banana"]

shift(): removes the first element from an array.

Example:

let fruits = ["apple", "banana", "orange"];
fruits.shift();
console.log(fruits); // ["banana", "orange"]

unshift(): adds one or more elements to the beginning of an array.

Example:

let fruits = ["banana", "orange"];
fruits.unshift("apple");
console.log(fruits); // ["apple", "banana", "orange"]

splice(): adds or removes elements from an array at a specified index.

Example:

let fruits = ["apple", "banana", "orange"];
fruits.splice(1, 1, "mango", "peach");
console.log(fruits); // ["apple", "mango", "peach", "orange"]

2. JavaScript String Methods:

indexOf(): returns the index of the first occurrence of a specified string in a string. Example:

let str = "Hello, world!";
console.log(str.indexOf("world")); // 7

slice(): extracts a section of a string and returns it as a new string. Example:

let str = "Hello, world!";
console.log(str.slice(0, 5)); // "Hello"

split(): splits a string into an array of substrings based on a specified separator. Example:

let str = "apple,banana,orange";
console.log(str.split(",")); // ["apple", "banana", "orange"]

replace(): replaces a specified value with another value in a string.

Example:

let str = "Hello, world!";
console.log(str.replace("world", "John")); // "Hello, John!"

3. JavaScript Math Methods:

ceil(): rounds a number up to the nearest integer. Example:

console.log(Math.ceil(4.2)); // 5

floor(): rounds a number down to the nearest integer. Example:

console.log(Math.floor(4.8)); // 4
  • round(): rounds a number to the nearest integer. Example:
console.log(Math.round(4.5)); // 5

random(): returns a random number between 0 and 1. Example:

console.log(Math.random()); // 0.123456789 (random number)

functions in JavaScript

  1. Math functions:
  • Math.abs(): returns the absolute value of a number. Example:
console.log(Math.abs(-5)); // 5
  • Math.max(): returns the maximum value of a list of numbers. Example:
console.log(Math.max(1, 2, 3)); // 3
  • Math.min(): returns the minimum value of a list of numbers. Example:
console.log(Math.min(1, 2, 3)); // 1
  • Math.sqrt(): returns the square root of a number. Example:
console.log(Math.sqrt(16)); // 4
  1. String functions:
  • String.fromCharCode(): returns a string created by using the specified sequence of Unicode values. Example:
console.log(String.fromCharCode(65, 66, 67)); // "ABC"
  • String.indexOf(): returns the index of the first occurrence of a specified value in a string. Example:
let str = "Hello, world!";
console.log(str.indexOf("world")); // 7
  • String.slice(): extracts a section of a string and returns it as a new string. Example:
let str = "Hello, world!";
console.log(str.slice(0, 5)); // "Hello"
  1. Array functions:
  • Array.isArray(): returns true if an object is an array, and false if it is not. Example:
let arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
  • Array.forEach(): executes a provided function once for each array element. Example:
let arr = [1, 2, 3];
arr.forEach(function(item) {
  console.log(item);
}); // 1, 2, 3
  • Array.map(): creates a new array by calling a provided function on each element in the array. Example:
let arr = [1, 2, 3];
let newArr = arr.map(function(item) {
  return item * 2;
});
console.log(newArr); // [2, 4, 6]
  • Array.reduce(): applies a function against an accumulator and each element in the array to reduce it to a single value. Example:
let arr = [1, 2, 3];
let sum = arr.reduce(function(accumulator, item) {
  return accumulator + item;
});
console.log(sum); // 6

How Does Mern Stack Work

How to encrypt a password in NodeJS?

2 thoughts on “JavaScript”

Leave a Comment

Skip to content