11 interesting things you should know about Javascript

Faraz Samin
2 min readMay 5, 2021
Photo by Artem Sapegin on Unsplash

1. Addition of string & number :

If you add a string to a number then it will be converted to a string as an output.

console.log('100' + 4)    // output : “1004”

Again,

console.log(100+100 + '4') // output : “2004”

At first, numbers are added, which is equal to 200, then it is added to a string ‘4’ which output is ‘2004’

2. ‘===’ operator :

Although the value of the two sides is same, ‘===’ returns false if the type is not same.

console.log(100==='100')   //falseconsole.log(‘100’==='100')   //true

3. Length of an array :

You can find how many elements in an array using the length property. It will return the number of elements in an array.

var arr=['programming','is','fun']console.log(arr.length)   // output : 3

4. Join two arrays :

Using concat() method, you can add two arrays to a new array. It does not change the current arrays.

var arr1=['programming','is','fun']var arr2=['Join','in','the','fun']var arr3 = arr1.concat(arr2)console.log(arr3) 
//["programming","is","fun","Join","in","the","fun"]

5. Find the position of an element in the array :

indexof() methods returns the position of an element in the array. If no elements are matched, then it returns -1.

var arr=['programming','is','fun']console.log(arr.indexOf('fun'))  //2

Javascript array is 0 index-based. So it has returned 2 as its position.

6. Adding new items to the end of an array :

push() method adds a new element to the end of the array.

var arr=['programming','is','fun']arr.push('ok?')console.log(arr)       // ["programming","is","fun","ok?"]

7. Adding new items to the beginning of an array :

unshift() method adds a new element to the beginning of the array.

var arr=['programming','is','fun']arr.unshift('ok?')console.log(arr)   // [“ok?",programming","is","fun"]

8. Slicing a string :

slice() method takes the start and end index of a string and gives the sliced string as an output. Indexing is 0 based.

var str = "programming"var sliced= str.slice(0,3)console.log(sliced)  // “pro”

It outputs string from starting index to ending index - 1.

9. Splitting a string :

split() method split the string into an array of substrings and it does not change the actual string.

var str = "programming"var split= str.split("")console.log(split) //   ["p","r","o","g","r","a","m","m","i","n","g"]

The string is split as a character as “ ” is used.

10. floor() method :

Returns the nearest lowest integer number as an output.

console.log(Math.floor(1.2))    //1console.log(Math.floor(-1.2))    //-2

11. ceil() method :

Returns the nearest highest integer number as an output.

console.log(Math.ceil(1.2))   //2console.log(Math.ceil(-1.2))   //-1

--

--

Faraz Samin

I am Faraz Samin, a Computer Science & Engineering final year student. I have experience in MERN stack Web Development and in Competitive Programming.