Things You Should Know About Advanced Javascript

Ruhana Binte Karim
3 min readMay 8, 2021

A small discussion

What are Truthy and Falsy Values?

  • If 0 is passed as a variable, it’s falsy whereas 1 will a truthy
  • Empty strings (“ ”) is a falsy
  • undefined, null, and NaN (Not a Number) are falsy
  • Empty array( [ ] ) and empty objects ({ }) are truth because hold some spaces in the memory.

Differences between Double equal(==) and Triple equal(===)?

Double equals in a conditional block check the values, if not then it tries to convert into the same criteria. But triple equals check both values and types.

const frst = 0;
const scnd = false;
if(frst == scnd){
console.log('true')
}
else{
console.log('false')
}
Output: true
const frst = 0;
const scnd = false;
if(frst === scnd){
console.log('true')
}
else{
console.log('false')
}
Output: false

What is Closure?

If you declare a function inside of another function then it creates a closed environment when you return it and sets an external value.

function counter(){
let count = 1;
return function(){
count++;
return count;
}
}
console.log(counter) // returns [function]
const count1 = counter()
console.log(count1()); // returns 2
console.log(count1()); // returns 3

What is an Event Bubble?

When you set an event with an element inside of other elements, all the elements get registered to that Event. The event is first captured then the other elements.

Event Bubble in Javascript

API, GET, and POST:

API ( Application Programming Interface) is a set of function from where you can have access to its data. If you access that data it’s a GET request whereas POST is just the opposite.

setTimeout and setInterval:

Javascript works synchronously but if you set a setTimeout it’ll execute the rest code before then execute itself. In other words, it becomes Asynchronous. setInterval is setting a time gap when you want to execute the code.

So, setTimeout( function , setInterval)

const doSomething = ()=> console.log(100)
console.log(200);
setTimeout(doSomething , 1000)
console.log(400);
Output: 200 , 400 , 100

‘this’ keyword:

If you call a method or function inside an object this keyword will refer to the object. It can refer to other objects as well depending on which context it is applied to.

const obj1 ={
name :'john',
getFullName: function(){
console.log(this);
return this.name;
}
}
obj1.getFullName();
const obj2 = {name:'harry'}
obj2.getFullName = obj1.getFullName
obj2.getFullName()
Output: { name: 'john', getFullName: [Function: getFullName] }
{ name: 'harry', getFullName: [Function: getFullName] }

Differences between bind, call, and apply methods?

The bind() method is used to bound an object with ‘this’ keyword when a function is called. Bind, call and apply methods are quite the same in the application except for the fact that bind() creates a copy of the function and then sets it with this keyword but call() and apply() method directly set the ‘this’ keyword and calls the function.

const data = {
num :10,
getNum: function(){
return this.num
}
}
const unBound = data.getNum;
console.log(unBound); // returns 10
const boundX = unBound.bind(data);
console.log(boundX()); // returns 10
const person = {
fullName: function (country) {
return this.firstName + " " + this.age + "," + country;
}
}
const person1 = {
firstName: "John",
age:56}
console.log(person.fullName.call(person1, "Bangladesh"))
Output: John 56,Bangladesh

const person = {
fullName: function (country) {
return this.firstName + " " + this.age + "," + country;
}
}
const person1 = {
firstName: "John",
age:56}
console.log(person.fullName.apply(person1, ["Bangladesh"]))
Output: John 56,Bangladesh

What are Global scope and Global variables?

A variable that is declared outside of a function and can be accessed from anywhere is a global variable. Global scope is where the global variable is declared.

var sub = 'math'    //Global variable in Global scopefunction multiply(num1, num2){
var result = num1 * num2;
console.log(sub);
function add (num){
return num + 5;
}
var total = add(result)
return total;
}
var res = multiply(10,20)
console.log(res, sub);
Output: 205 math

--

--