DEV Community

Vasanth S
Vasanth S

Posted on

Essential JavaScript Methods You must Know

JavaScript provides many built-in methods that make coding easier. Here are some of the most commonly used methods with their definitions, syntax, and examples.

toUpperCase() - Converts a string to uppercase letters.

Syntax :
string.toUpperCase()

Example :

let name = "hello";
console.log(name.toUpperCase()); // Output: "HELLO"
Enter fullscreen mode Exit fullscreen mode

toLowerCase() - Converts a string to lowercase letters.

Syntax :
string.toLowerCase()

Example :

let name = "HELLO";
console.log(name.toLowerCase()); // Output: "hello"
Enter fullscreen mode Exit fullscreen mode

trim() - Removes whitespace from both ends of a string.

Syntax :
string.trim()

Example :

let text = "   Hello World!   ";
console.log(text.trim()); // Output: "Hello World!"
Enter fullscreen mode Exit fullscreen mode

includes() - Checks if a string contains a specific substring.

Syntax :
string.includes(searchValue)

Example :

let sentence = "JavaScript is powerful";
console.log(sentence.includes("powerful")); // Output: true
Enter fullscreen mode Exit fullscreen mode

push() - Adds new elements to the end of an array.

Syntax :
array.push(element1, element2, ...)

Example :

let numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers); // Output: [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

pop() - Removes the last element from an array and returns it.

Syntax :
array.pop()

Example :

let fruits = ["apple", "banana", "mango"];
fruits.pop();
console.log(fruits); // Output: ["apple", "banana"]
Enter fullscreen mode Exit fullscreen mode

shift() - Removes the first element from an array.

Syntax :
array.shift()

Example :

let colors = ["red", "green", "blue"];
colors.shift();
console.log(colors); // Output: ["green", "blue"]
Enter fullscreen mode Exit fullscreen mode

unshift() - Adds new elements to the beginning of an array.

Syntax :
array.unshift(element1, element2, ...)

Example :

let cars = ["BMW", "Audi"];
cars.unshift("Tesla");
console.log(cars); // Output: ["Tesla", "BMW", "Audi"]
Enter fullscreen mode Exit fullscreen mode

slice() - Returns a shallow copy of a portion of an array or string.

Syntax :
array.slice(start, end)

Example :

let animals = ["cat", "dog", "rabbit", "lion"];
console.log(animals.slice(1, 3)); // Output: ["dog", "rabbit"]
Enter fullscreen mode Exit fullscreen mode

splice() - Adds/removes elements from an array.

Syntax :
array.splice(start, deleteCount, item1, item2, ...)

Example :

let items = ["pen", "pencil", "eraser"];
items.splice(1, 1, "marker");
console.log(items); // Output: ["pen", "marker", "eraser"]
Enter fullscreen mode Exit fullscreen mode

map() - Creates a new array by applying a function to each element.

Syntax:
array.map(callback)

Example :

let nums = [1, 2, 3];
let squared = nums.map(n => n * n);
console.log(squared); // Output: [1, 4, 9]
Enter fullscreen mode Exit fullscreen mode

filter() - Returns elements of an array that satisfy a condition.

Syntax :
array.filter(callback)

Example :

let ages = [12, 18, 25, 30];
let adults = ages.filter(age => age >= 18);
console.log(adults); // Output: [18, 25, 30]
Enter fullscreen mode Exit fullscreen mode

reduce() - Reduces an array to a single value.

Syntax :
array.reduce(callback, initialValue)

Example :

let nums = [1, 2, 3, 4];
let sum = nums.reduce((total, n) => total + n, 0);
console.log(sum); // Output: 10

Enter fullscreen mode Exit fullscreen mode

forEach() - Executes a function for each array element.

Syntax :
array.forEach(callback)

Example ::

let fruitsList = ["apple", "mango", "orange"];
fruitsList.forEach(fruit => console.log(fruit));
Enter fullscreen mode Exit fullscreen mode

sort() - Sorts elements of an array.

Syntax :
array.sort(compareFunction)

Example :

let numbersList = [4, 2, 9, 1];
numbersList.sort((a, b) => a - b);
console.log(numbersList); // Output: [1, 2, 4, 9]
Enter fullscreen mode Exit fullscreen mode

indexOf() - Returns the index of the first occurrence of an element.

Syntax :
array.indexOf(element)

Example :

let langs = ["JS", "Python", "C"];
console.log(langs.indexOf("Python")); // Output: 1
Enter fullscreen mode Exit fullscreen mode

replace() - Replaces a substring with another in a string.

Syntax :
string.replace(searchValue, newValue)

Example :

let greet = "Hello World";
console.log(greet.replace("World", "JS")); // Output: "Hello JS"
Enter fullscreen mode Exit fullscreen mode

So here are the methods most commonly used by developers. Thanks for reading! See you soon with another exciting topic.

Top comments (0)