JavaScript is a programming language that is commonly used in web development. It is used to create and control dynamic content on the web, such as updating content on a website in real time, creating interactive forms and animations, and more.
JavaScript is a high-level, interpreted language, which means that it is easy to learn and use, but it also has a lot of power and flexibility. It is an essential tool for web developers, as it allows them to build interactive and engaging websites and applications.
But, which are the commonly used methods in JavaScript?
.forEach()
It executes a provided function once for each array element
let fruits = ['apple', 'banana', 'mango']; fruits.forEach(function(fruit) { console.log(fruit); });
.map()
It creates a new array with the results of calling a provided function on every element in the calling array
let numbers = [1, 2, 3, 4, 5]; let doubleNumbers = numbers.map(function(number) { return number * 2; }); console.log(doubleNumbers); // [2, 4, 6, 8, 10]
.filter()
It creates a new array with all elements that pass the test implemented by the provided function
let numbers = [1, 2, 3, 4, 5]; let evenNumbers = numbers.filter(function(number) { return number % 2 == 0; }); console.log(evenNumbers); // [2, 4]
.reduce()
It applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value
let numbers = [1, 2, 3, 4, 5]; let sum = numbers.reduce(function(accumulator, currentValue) { return accumulator + currentValue; }, 0); console.log(sum); // 15
.concat()
It returns a new array that is the result of concatenating the given arrays or values
let fruits = ['apple', 'banana']; let vegetables = ['carrot', 'potato']; let produce = fruits.concat(vegetables); console.log(produce); // ['apple', 'banana', 'carrot', 'potato']
.includes()
It determines whether an array includes a certain value among its entries, returning true or false
let fruits = ['apple', 'banana', 'mango']; console.log(fruits.includes('banana')); // true console.log(fruits.includes('pear')); // false
.indexOf()
It returns the first index at which a given element can be found in the array, or -1 if it is not present
let fruits = ['apple', 'banana', 'mango']; console.log(fruits.indexOf('banana')); // 1 console.log(fruits.indexOf('pear')); // -1
.join()
It joins all elements of an array into a string and returns the string
let fruits = ['apple', 'banana', 'mango']; let fruitString = fruits.join(', '); console.log(fruitString); // "apple, banana, mango"
.split()
It splits a string into an array of substrings and returns the new array
let str = "apple, banana, mango"; let fruits = str.split(", "); console.log(fruits); // ["apple", "banana", "mango"]
These are some of the commonly used JavaScript methods, but if you want to learn more about JavaScript you can check out this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript
Image by Freepik