Boost JavaScript tricky things!

Fayed Al Mamun
4 min readNov 5, 2020
  1. Truthy and Falsy : Now, seeing this point anyone can get confused that what actually truthy or falsy means. Let me clear this, 1st of all truthy means what a condition return ,if the condition is return true then it will call truthy.On the other hand if it return false then it will call falsy.An example will clear it:
let number =99;
if (number){
console.log('truthy')
} // any non-Zero value of number is truthy
let science='Physics'
if(science){
console.log('truthy')
}// any value of science except empty value is truthy

if(undefined){
console.log('falsy')
}// undefined is falsy
if(null){
console.log('falsy')
}// null is falsy

2. Undefined Vs Null : If any one unintentionally, some time intentionally forget to define a variable will return undefined. There are many reason to get undefined value. Some are given bellow:

let love;
console.log(love)// undefined
function pureLove(){
const notFound='404 Error'
return notFound;
}
console.log(notFound) // undefined
function peace(beingSingle,beingMingle){
console.log(beingMingle);//undefined
}
peace(true)

Now, null value will return when a developer intentionally set a variable null. That is the main difference between null and undefined.

3. Double Equal (==) Vs Triple Equal (===) : Both double equal and triple equal used to check a condition either the condition is true or false. Now,lets talk about the difference, when a javascript programmer use double equal to check a condition it only check the value, more specifically it will compare only the value on the other hand using triple equal is more secure checking process. It will check value as well as the type if any of two is false then result will false. An example given bellow:

let number= 10;
let numberStr='10'
if(number==numberStr){
//the condition will return true though the type is not same
}
if(number === numberStr){
//the condition will return false because the type is not same

4. Bind() : If a developer want to use a method of a object in a new object,then he/she can do it by the help of bind. Bind is as like as adding method. Bind will return a function. An example will clear it:

const poorPerson= {
fName:'abul',
salary:150,
gf: false,
getGf:function(gf){
this.gf=gf
return this.gf
}
const richPerson={
fname:'babul',
salary:150000,
gf: false
}
const richPersonGF=poorPerson.getGf.bind(richPerson)
richPersonGf(true)
console.log(richPerson.gf)//true.

5. Call(): If a developer want to use a method of an object directly in a new object then he/she can use call(). An example is given bellow:

const poorPerson= {
fName:'abul',
salary:150,
gf: false,
getGf:function(gf){
this.gf=gf
return this.gf
}
const richPerson={
fname:'babul',
salary:150000,
gf: false
}
poorPerson.getGf.call(richPerson,true)
console.log(richPerson.gf)//true.

6. Apply(): Apply() function works as like as call() function but there is a little bit of difference , in apply() function arguments are given as Array. An example is given bellow:

const poorPerson= {
fName:'abul',
salary:150,
gf: false,
getGf:function(gf){
this.gf=gf
return this.gf
}
const richPerson={
fname:'babul',
salary:150000,
gf: false
}
const richPersonGF=poorPerson.getGf.apply(richPerson,[true])
console.log(richPerson.gf)//true.

7. Bind() Vs Call() Vs Apply() : In bind() it will return a function it can not be used directly to a new object.To use it one has to create a function 1st. But in call() and apply() both of them can be used directly in a new object,but the difference is in the passing arguments. In call() arguments are passed as comma separated way but in app() arguments are pass as a array. In the bellow an example is given:

// For bind()
const richPersonGF=poorPerson.getGf.bind(richPerson)
richPersonGf(true)
console.log(richPerson.gf)//true.
//For call()
poorPerson.getGf.bind(richPerson,true)
console.log(richPerson.gf)//true.
//For apply()
poorPerson.getGf.bind(richPerson,[true])
console.log(richPerson.gf)//true.

8. Scope in javascript: Scope is a block area in where a variable is defined.If a variable is defined in global scope it will defined everywhere.But if a variable is defined inside a function it will not defined out side of that function that is called local scope. Here is an example:

let globalAge=10;function getAge(bithYear){ const localAge=20; return globalAge+birthYear // here inside the function globalAge is defined.Because globalAge is defined in global scope
}
//here localAge is not defined because localAge is defined in local scope.

9. Closures: If a function return/ call another function that will create a close environment . And if the inside function access a variable of outside function then it will create individual value for each call just like it makes a close environment.That is call closures. For example:

function watch(){
let time=0;
return function getTime(){
time++
return time;
}
}
const watch1=watch();
const watch2= watch()
console.log(watch1)//1
console.log(watch1)//2
console.log(watch1)//3
console.log(watch2)//1
console.log(watch2)//2

10. Class vs Object & ‘new’ Keyword: If a developer need a bundle of object of same category he/she will not create same type object again and again rather he/she will create a Class of that same pattern . From this class he/she can create objects in number of his/her need. Now, what is the need of ‘new’ keyword, lets talk about it! To create an object from a class ‘new’ keyword has to be used. For an example:

class Male{
constructor(fName,lName,salary){
this.fName=fName;
this.fName=lName;
this.fName=salary;
}
}
const poorPerson=new Male('Abul','kamal',1000)
console.log(poorPerson);
const richPerson=new Male('Babul','lamal',10500)
console.log(richPerson);

--

--