10 Methods Of JavaScript
So let’s get started.
……………………………………….
1. How to Split a String into an Array in JS
- Split() method is used to split a string separately. String is split between each character. If you want to split a string with multiple separators then you have to pass regex expressions into a split method. That’s how we work with split method in JavaScript. The string.split() method does not change the original string.
Example :
let story = “I will tell the story”;
console.log(story.split(“ ”));
Output : [“I”, “will”, “tell” “the” “story”];
2. String.toLowerCase() Method
string.toLowerCase() method is used to convert a string to lowercase letters. String toLowerCase() is a built-in JavaScript method that returns the calling string value converted to lowercase. The string.toLowerCase() method does not change the original string.
Example :
let story = “I WILL TELL THE STORY”;
console.log(story.toLowerCase(“ ”));
Output : [“i will tell the story”];
3. String.toUpperCase() Method
string.toUpperCase() method is used to convert a string to uppercase letters. String toUpperCase() is a built-in JavaScript method that returns the calling string value converted to uppercase. The string.toUpperCase() method does not change the original string.
Example :
let story = “i will tell the story”;
console.log(story.toUpperCase(“ ”));
Output : [“I WILL TELL THE STORY”];
4. string.replace() Method
string.replace() method is used to replace a part of the given string with some other string or a regular expression. string.replace() returns a new string where the specified values are replace. When we need to replace some letter or sentence, we simply use string.replace() method. The string.replace() method does not change the original string.
Example :
const pet= ‘I love cats’;
console.log(pet.replace(‘cats’, ‘birds’));
Output : [“I love birds”];
5. String.trim() Method
string.trim method is used to remove whitespace from both sides of a string. The trim() is a built-in string function in JavaScript. When we need to remove a whitespace from characters, we simply use string.trim() method. The trim() method does not change the original string.
Example :
const pet= “ I love cats ” ;
console.log(pet.trim(“”));
Output : [“I love cats”];
6. String.trimStart() Method
string.trimStart method is used to remove whitespace from the beginning of a string. The trimStart() is a built-in string function in JavaScript. When we need to remove a whitespace from the beginning characters, we simply use string.trimStart() method. The trimStart() method does not change the original string.
Example :
const greeting= “ hello!” ;
console.log(greeting.trimStart(“”));
Output : [“hello”];
7. String.trimEnd() Method
string.trimEnd method is used to remove whitespace from the end of a string. The trimEnd() is a built-in string function in JavaScript. When we need to remove a whitespace from the end characters, we simply use string.trimEnd() method. The trimEnd() method does not change the original string.
Example :
const greeting = “ hi! ” ;
console.log(greeting.trimEnd(“”));
Output : [“hi”];
8. math.floor() Method
math.floor method is used to return the largest integer value that is less than or equal to a number. In other words, the floor() function rounds a number down and returns an integer value. The math.floor() is a built-in function in JavaScript. floor() is a static method of Math, you always use it as Math.floor().
Example :
let number = 5.567
let result = math.floor(number)
console.log(result);
Output : 5 ;
9. array.push() Method
array.push() method is used to add items to the end of an array and returns the new length. . The array.push() is a built-in array function in JavaScript. When we need to add items to the end of an array we use array.push method. it is such a easy way to add items to the end of an array.
Example :
const animals = [ “cat”, “snake”];
const count = animals.push(“dog”);
console.log(animals);Output : [“cat”, “snake”, “dog”];
10. array.pop() Method
array.pop() method is used to remove items to the end of an array and decrements the length parameter. The array.pop() is a built-in array function in JavaScript. When we need to remove items to the end of an array we use array.pop method. It is such an easy way to remove items to the end of an array.
Example :
const animals = [ “cat”, “snake”];
const count = animals.pop(“ ”);
console.log(animals);Output : [“cat”];