In my point of view, Length is the most used method in any programming language. Using the Length method we can find out how many characters exist in a string. Not only string, but we can also use this method for array and other data types. Suppose you want an input text including 6 characters from users. It is not necessary that all the users will give a text including 6 characters, so we need to validate whether it is 6 characters or not using the Length method.
var userInput = “abcdef”userInput.length // output 6var userInput = “abced”userInput.length // output 5
parseInt() / parseFloat()
parseInt and parseFloat are another most important method in programming. By this method, we can convert a string data type to a number data type. For example, you need an input number from users and you will add 10 with it. Suppose, one user gives an input number 20. So, result will be 20 + 10 = 30. What if the user gives a string number like this “20”. If you add 10 with “20” the output will be NaN. In this situation, you need to convert “20” to 20 using parseInt. You can also convert a decimal string to a number like “20.22” to 20.22.
var userInput = 5var result = userInput + 10 //output 15var userInput = “5”var result = userInput + 10 //output NaN
var userInput = 5typeof userInput //output numbervar userInput = ‘abcd’typeof userInput //output string
var description = “Hello, my name is Ahnaf Abir. I am a web developer”description.includes(“Banker”) // output falsevar description = “Hello, my name is Ahnaf Abir. I am a web developer”description.includes(“developer”) // output true
toLowercase / toUppercase
These methods are one of the most important methods for the string data type. By these two methods, we can convert a string to lowercase or uppercase. Suppose, you want to get a username from a user and we all know that username always is in lowercase. But the user can also give a username that is in uppercase like this “AHNAFABIR11”. So, we need to convert this username to this “ahnafabir11” using the toLowerCase method.
var username = “AHNAFABIR11”username.toLowerCase() // output ahnafabir11
trim, trimStart, trimEnd
var userInput = “ Hello, World ”console.log(userInput) // output “ Hello, World ”var userInput = “ Hello, World ”userInput.trim() // output “Hello, World”
var x = “hello”x + 10 // output NaNNumber.isNaN(NaN) // output trueNumber.isNaN(“hello”) // output falseNumber.isNaN(1) // output false
ceil / floor / round
ceil, floor, and round are the most important methods for Number data types. There are two types of number integer and float. 102 is an integer number and 102.324 is a float number. Sometimes you need to make a float number to an integer. Suppose, you have a float number like this 32.45, you need to make whether 32 or 33. It will be easy to understand seeing the example below.
var number = 32.45Math.ceil(number) // output 33Math.floor(number) // output 32var number = 15.49 // < 15.50Math.round(number) // output 15var number = 15.56 // > 15.50Math.round(number) // output 16
push / pop
var animals = [‘dog’, ‘cat’]animals.push(‘cow’) // output [‘dog’, ‘cat’, ‘cow’]var animals = [‘dog’, ‘cat’]animals.pop() // output [‘dog’]
shift / unshift
Previously, we talk about push and pop. shift and unshift simply the opposite of push and pop. pop and push work with the end position of an array, on the other hand, shift and unshift work with the first position of an array. The example below will help you to understand better -
var animals = [‘dog’, ‘cat’, ‘cow’]animals.shift() // output [‘cat’, ‘cow’]var animals = [‘dog’, ‘cat’, ‘cow’]animals.unshift(‘bird’) // output [‘bird’,‘dog’, ‘cat’, ‘cow’]