Top 10 Important Things of JavaScript

Teertha Dev Sarker
5 min readMay 8, 2021

1. Truthy and Falsy Values-

When we check condition we must depend truthy or falsy values. So what is truthy and falsy values?
We condition matched then it return truthy value and when condition not matched then return falsy value.

This is a simple example check condition

let num=12;if(num){console.log("true");}else{console.log("false");}

In this condition we just check values are exists or not in num variable? At first we declared 12 in num that means the num variable did not empty. So must be condition was true that mean print true.

let num=0;if(num){console.log("true");}else{console.log("false");}

In this condition we declared 0 in num. 0 means there are no values in variable so It will print false. If we declared empty string it also print false.

2. double equal (==) vs triple equal (===)-

When we check between two variable’s values are equal or not then we use double equal.

let num1= 5;let num2= "5";if(num1==num2){  console.log("true");}else{  console.log("false");}

In this example we check just two values are equal or not. Here’s num1 is a number type value and num2 is a string type value. But when we use double equal it just check values. It’s not check there type. So it will print true. If you have any confusion you can compile this code.

let num1=5;let num2= "5";if(num1===num2){  console.log("true");}else{  console.log("false");}

In this code we used triple equal. That means it’s not only check values it also check there’s data type. So we can clearly see num1 has number type values and num2 has string type values so both data types are not same so it will print false because the condition is not true.

3. Scope-

Scope is a area of variables. When we declared any variable in function we cannot access that variable outside of function we can access only inside of function this is a scope. When we declared any variable in function that means we set the variable’s scope in function the variable cannot accessible outside of function. But if we declared global way then we can access it anywhere.

function print(){  let text = "Hello World";}print();console.log(text)

In this example we declare a variable in a function if we try to print text then it will show error because when we declare text in function then we also set this scope. It only access with function area. If we access this variable we must return this.

function print(){let text = "Hello World";return text;}console.log(print())

Then we can print the whole function call like the example.

4. Closure-

When we write a function we return value. Values like string, number, object, array anything but if we return a function it will create a closed environment. That means we can call this function in multiple way.

function watch(){ let count = 0;  return function(){     count++;     return count;   }}const clock1 = watch();console.log("I'm Clock 1 : ",clock1())console.log("I'm Clock 1 : ",clock1())console.log("I'm Clock 1 : ",clock1())const clock2 = watch();console.log("I'm Clock 2 : ",clock2())console.log("I'm Clock 2 : ",clock2())

In this example we just declared count variable in watch function then simply return another function then in this function we just increase count variable then return it. Then simply we declared a clock1 variable then call watch function then print clock1 function. Also we declared clock2 variable then call watch function then print clock2 function. You can see the output.

You clearly see the output when we print clock1 it’s print one way then when we print clock2 it print another way. That mean we call same function but we can use it different way of different variable that is called closure.

5. bind() Method-

When we share any object property to another object then we can use this method.

const obj1 = {  num1: 3,  num2: 4,  add: function(){    return this.num1+this.num2;  }}const obj2 = {  num1: 5,  num2: 10}const result = obj1.add.bind(obj2);console.log(result()); // 15

We can just declare two function. In obj1 function has another function in property add. We just simply return this.num1+this.num2. this keyword basically indicate obj1's num1 and num2 that means this is own property.

When we declare obj2 object then it has no add property function but we want to use add method but the add property is available on obj1. So Using bind method we just get a add property from obj1. Remember bind method always return a function that means we need to call it. When we bind add with obj2 then this keyword basically indicate obj2’s num1 and num2. So the output will be 15.

6. call() Method-

Call method is short cut way of bind method. bind() basically return a function then we need to call them but call method is direct it not return anything we just direct access from object.

const obj1 = { num1: 3, num2: 4, result: null, add: function(){  this.result = this.num1+this.num2  return this.result;  }}const obj2 = {  num1: 5,  num2: 10,  result: null}obj1.add.call(obj2)console.log(obj2.result);

Another Example for pass parameter in function:

const obj1 = {result: null,add: function(n1,n2){this.result = n1+n2;return this.result;}}const obj2 = {result: null}obj1.add.call(obj2 , 15 , 20)console.log(obj2.result); // 35

In this example we can pass parameter in call method for add function.

7. apply() Method-

Apply is similar to call method but some change in pass parameter.

const obj1 = {result: null,add: function(n1,n2){this.result = n1+n2;return this.result;}}const obj2 = {result: null}obj1.add.apply(obj2 , [15,20] )console.log(obj2.result);

When we pass parameter for add function we must pass as a array in apply method.

8. Asynchronous Javascript-

Basically JavaScript follow synchronous way. When we write any code which code we write first that code will execute first that means javaScript compile step wise.

console.log(“Hello”);console.log(“World”);

In this code we write first “Hello” then we write “World” so first print “Hello” then it will print “World” that is synchronous way.

But if we want then we can use asynchronous way in javaScript

setTimeout(function(){console.log("Hello");})console.log("World");

Now it will first print “World” then print “Hello”. This is the simple way to make Asynchronous way in javascript.

9. Recursion-

When a function call by own then we can say that is recursion function.

function countDown(number) {console.log(number);const newNumber = number - 1;if (newNumber > 0) {countDown(newNumber);}}countDown(10);

In this example countDown function is call own.

10. let const ( ES6 )-

Before ES6 release we use var but after ES6 provide let and const and it’s more efficient way to declare a variable.

When we can declare a changeable variable then we can use let

let number = 6;
number = 3;

We can change it.

If we can declare a fixed variable then we can use const-

const number = 6;

We cannot change this variable if you try to change this then you will get error.

--

--