10 Important thing about JavaScript

Array length

let var x=[1,2,3,4]

console.log(x.length) // 4

Normally we think the length of an array will be the number of items in the array. But it’s not true all time. Lets clear with an example:

x.[100]=21;

console.log(x.length) // 101

In this case length of the array is 101 but we see there are only four elements in the array.

That means array length will be the highest index number + 1.

Double equal vs Triple equal

123 == “123” (true)

This is a confusing thing in JavaScript. To compare normally we use double equal. Double equal just compare the value that’s why 123 == “123” (true). To avoid type correction we need to use triple equal Because triple not only checks the value it also checks the type.

Math.floor and Math.round

Math.floor and Math.round almost the same they all convert a float or double number into integer but in a different way.

Normal,

var x = 5/2;

console.log(x) // 2.5

Let’s see how Math.floor works,

console.log(Math.floor(x)) // 2

Result is 2;

And what’s hapened in Math.round

console.log(Math.round(x)) // 3

The result is 3. That means Math.round converts float and number with the nearest upper value and Math.floor converts float and double number with the lower nearest number.

concat()

String concatenation is one of the common things in any programing language. String concatenation means to add two or more strings and return a new string. Const str1 = “hello” Const str1 = “world” Console.log(str1.concat(str2)) // “hello world”

replace()

replace() method replaces apart from a string and it returns a new String.

Let,

var x = “hello world”

console.log(x) // “hello world”

var replace = x.replace(“world”,”mars”);// returns “hello mars”

find()

find() method will find the first value returns the value from the array. If it gets no passed value then it will return undefined.

var array = [5, 12, 8, 130, 44];

var found = array.find(element => element > 10);

console.log(found) // 12

Because 12 is the only first element that matches with the condition and passed the testing function.

charAt()

String chatAt() method is used to find a specific character from a string or we can say it find specific indexes value.

let ,

var x=’hab’;

console.log(x.charAt(2)) // a

toLowerCase()

This method converts the string value to lowercase letters and the output string to lowercase .

Let,

var name = ‘VXXX’;

console.log(name.toLowerCase()) //‘vxxx’

toUpperCase()

This method converts the string value to uppercase letters and the output string to uppercase .

Let’s consider : var x = ‘vxxx’;

console.log(x.toUpperCase()) //‘VXXX’

indexOf()

indexOf() is used to find a specific strings index number.

var c = [‘yh’,’i’,’b’]

console.log(c.indexOf(‘i’)); //1