Some Important methods of Javascript, mostly useable…
2 min readMay 5, 2021
Javascript String, Number, Math
- indexOf(): In an array or object to find out number or word’s position, we use indexOf. If the indexOf’s value is -1 that means it doesn’t find anything like what is searching. Example:
const friends =[‘Rahim’, ‘Karim’, ‘Kamal’, ‘Jamal’, ‘Abul]; console.log(friends.indexOf(‘Kamal’));
Output is: 2; - replace(): If we need to change any word or number in an array or object, we can easily change it by replace() method. Example:
const comment = “The quick brown fox jump over the lazy dog”; console.log(comment.replace(‘dog’, ‘cat’));
Output is: ‘The quick brown fox jump over the lazy dog.’ - slice(): Sometimes we want to keep some particular items removing others items. Then we can do that by slice(). Example:
const fruits = [‘banana’, ‘apple’, ‘orange’, ‘watermelon’, ‘mango’]; console.log(fruits.slice(2));
Output is: [‘orange’, ‘watermelon’, ‘mango’] - toUppercase(): Making capital letter our article’s all words or any specific word toUppercase is the simple way.
const sentence = ‘Bangladesh is my homeland’; console.log(sentence.toUppercase());
Output is: ‘BANGLADESH IS MY HOMELAND’; - toLowercase(): To convert all words into smaller word we can use toLowercase. Example:
const sentence = “I love Bangladesh”; console.log(sentence.toLowercase());
Output is: “i love bangladesh”; - isNaN: Calculating any math if we give any input against number to letter which means nothing then output comes NaN(not a number). Example:
var x = 5;
var y = a;
console.log(x/y);
Output is: NaN; - parseFloat(): Convert number into string, parseFloat is used. If we use parseFloat, it calculate the all inputed funtionality then make the result into string. Let’s have a look by a example:
function result(num){
parseFloat(num) * 5;
console.log(result(10))
Output is: ‘50’
} - parseInt(): parseInt() is used to covert any string into integer number. Example:
var a = ‘20.12’;
result = (parseInt(a));
console.log(result);
Output is: 20; - Math.floor(): In this method decimal number converts into less integer number. If we use Math.floor() in a decimal number, it reduces the all decimal number after the integer number and return just integer number. Example:
var x = 12.12;
result = Math.floor(x);
console.log(result);
Output is: 12; - Math.ceil(): It converts decimal number into more integer number. If we use Math.ceil() in a decimal number, it increases the decimal number and returns larger integer number. Example:
var x = 12.14;
result = Math.ceil(x);
console.log(result);
Output is: 13;