As it’s worldwide popular, it has a vast library which helps you to do anything and any way you want. Let’s talk about some of the essential features of it.
- The Problem with Integer/Number
So in case you want only the integer part (and not the part after decimal point), you can use Math.floor() function. An example -
2. Converting a String to an Integer
This is something very interesting and also essential. There can be a lot of times when you might have got a number from an object or an input field and it’s in a string format, what would you do then? To solve such a problem, parseInt() is a very essential function.
The interesting part is, while parsing the string, if you get a non-decimal number, you can easily convert it to decimal as well.
Here ‘base’ is an argument where you can declare which number system that number is based on. Let’s say you got a number 177 which octal and you want to convert it in decimal format.
But another similar function parseFloat() always get the base 10, not anything else so be careful with that.
3. How long is the Word/Sentence/Paragraph/Essay?
We often need to try loop on a string property and for that we need to know how long it can be. So here is a solution for you, stringName.length. You can try it on any string even if it’s a huge essay and it will say how many characters are there. But one thing you need to remember that spaces, white spaces and punctuation marks are also considered as characters.
4. What if you don’t want the whole string but a part of it?
5. You want to split things from a specific position?
“I am very tired and hungry” — Let’s say from here, you want every word individually without the space. Using slice will be tiresome work. So you can split() the sentence whenever there will be a space. And our code will be -
As we can see, the result will be an array of subarrays of words. Cool huh?
6. Adding two Strings
Let’s say you have two strings and you want to add them. As an example, “This is an example” and “of adding two strings”. If you use ‘+’ operator like algebraic function, they will be added. The code will look like -
That little empty string is for adding a space in output. Try removing, you might understand!
7. Truth or False!
This is actually a boolean expression. Let’s say you want to check if a number is greater than 7 or not and if it is greater, you want to print “big number”.
The output is desired because here the statement is True.
But the fun fact is, Truth or False doesn’t stop here rather their capability is much vast. Such as any empty string, NaN (Not a Number), Null and Undefined values are false. And expectedly the opposites are True.
Array is a special type of object and very much used. There are numerous methods for array. We will discuss a few here.
8. Removing from Start or End
Let’s say you want to remove an item from an array and it’s either the first element or the last. To Remove from the first position, you should use the shift() method and to remove from the very last position, you should use the pop() method. Example -
9. What about modifying an array from a specific position?
splice() is a very flexible and useful method when it comes to modifying an array. You can delete items from a specific position to a specific number of elements and you can add some new items as well!
The syntax looks like this — arrayName.splice(starting_index, how_many_items_to_be_deleted, newItem1, newItem2,….newItemN)
And if you don’t want to add any item rather just delete then syntax would be like this — arrayName.splice(starting_index, how_many_items_to_be_deleted)
Let’s see and example -
10. Trying to reverse everything?
reverse() method is very handy in certain times. Usually it needs a few lines of code to perform the action but this little method can save you from that trouble. Example
That’s all for today. Hope to see you soon in another blog!