10 useful methods of javascript
JavaScript is often derided as a toy language. But after releasing ES6 it becomes more powerful. Nowadays, It is being widely used in every field of modern technology. From server-side to client, It is very popular with web developers. JavaScript has some awesome built-in functions. today I will discuss ten functions of them
- Number.isNaN
We know NaN is the special data type of javascript. When we calculate two numbers if one of them is not a valid number, then the calculation result returns NaN. In this situation, we can check if the result valid number or not.
console.log(isNaN(‘a’*10)) // true
2. Number.parseInt
Sometimes, we need to parse numbers from strings. Also, we can convert from various base numbers by using this method.
console.log(Number.parseInt('10', 2)) // 2
//10 is a binary form of 2
3. String.includes
It returns if the string contains the provided value. Remember that, this method is case sensitive.
const str = 'I love Bangladesh'
console.log(str.contains('Bangla')) // true
console.log(str.contains('bangla')) // false
4. String.replace
It replaces the first matched part of the string with the given input.
const str = 'Hello World'
console.log(str.replace('World', 'Earth')) // Hello Earth
5. String.replaceAll
It replaces all matched parts of the string with the given input.
const str = 'We are two brothers and two sisters'
console.log(str.replaceAll('two', 'three'))
// We are three brothers and three sisters
6. String.toLowerCase
It converts all characters of a string to lowercase.
const name = 'Bill Gates'
console.log(name.toLowerCase()) // bill gates
7. String.toUpperCase
It converts all characters of a string to uppercase.
const name = 'Melinda Gates'
console.log(name.toUpperCase()) // MELINDA GATES
8. String.trim
This method removes white space from a string.
const str = ' I love Bangladesh '
console.log(str.trim()) // 'I love Bangladesh'
9. String.slice
It returns a section of a string by the given index number without modifying the original string
const str = 'I love Bangladesh'
console.log(str.slice(7)) // Bangladesh
10. String.split
It divides a string into an ordered list of substrings according to the given first parameter, puts these substrings into an array, and returns the array.
const str = 'I love Bangladesh'
console.log(str.split(' ')) // ['I', 'love', 'Bangladesh']
That’s all for today, Thanks for reading.