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"
toLowerCase() - Converts a string to lowercase letters.
Syntax :
string.toLowerCase()
Example :
let name = "HELLO";
console.log(name.toLowerCase()); // Output: "hello"
trim() - Removes whitespace from both ends of a string.
Syntax :
string.trim()
Example :
let text = " Hello World! ";
console.log(text.trim()); // Output: "Hello World!"
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
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]
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"]
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"]
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"]
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"]
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"]
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]
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]
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
forEach() - Executes a function for each array element.
Syntax :
array.forEach(callback)
Example ::
let fruitsList = ["apple", "mango", "orange"];
fruitsList.forEach(fruit => console.log(fruit));
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]
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
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"
So here are the methods most commonly used by developers. Thanks for reading! See you soon with another exciting topic.
Top comments (0)