DEV Community

Cover image for 10 facts about Javascript
Asikur Rahman
Asikur Rahman

Posted on

10 facts about Javascript

1.Character access in string
There are two way to access characters of the string, one is using charAt()
Alt Text

The other way is just like an array-like object to access each character of the string.
Alt Text

2.Find the index of string
By using the indexOf() function we can easily find the first matching index number of a string. This function mainly return the index of the first matching string.
Alt Text

3.Changing the case of a string.
If we want to convert an upper case letter into a lower case then we dont have to convert it by using ASCII value like in C. Javascript has two beautiful function to do that toUpperCase() convert lower case letter into upper case and toLowerCase() do the reverse of toUpperCase().
Alt Text

4.String to integer/float
There are two functions in javascript which allow us to parse a string into an integer or float in one second which is parseInt() and parseFloat().
parseInt() take two parameters first one is the string and the second one is the base of the number system while parseFloat() do it for only the decimal number system and take only one parameter.
Alt Text

  1. Random numbers in javascript Math.random() return a floating number range from 0 to less than 1. So if we want any fixed ranged random number than we have to multiply Math.random() with what we want. Suppose if we want a random number from 1 to 6 than we can do this Alt Text

6.Loop over an array
If we want to access each element of an array then we can use forEach() loop which is much easier and shorter than for loop.
Alt Text

7.Inserting element and removing element from the array
Using push() we can insert a new element in the array and by pop() we can remove the last element from the array
Alt Text

8.Difference between find() and filter()
By using filter() we get a portion of that array but using find() we get an element.
Alt Text

9.Difference between slice() and splice()
The slice() method returns the selected items as a new array object and don’t change the original array while splice() method returns the original array with removed items.
Alt Text

  1. Difference between shift() and unshift() The shift() method remove an element at a specific position and shifts the remaining elements up while returns the removing element In the meantime unshift() exactly done the opposite thing it adds an element at a specific position and shifts the remaining element down and return the size of the array. Alt Text

Top comments (0)