Wash your brain with JavaScript

Jahid Hasan Siam
4 min readMay 8, 2021

--

1. Truthy Vs Falsy

Ans: In JavaScript truthy value is considered true as a Boolean context and falsy value in considered false. All values are truthy value if we don’t
define them false. Here are some examples of truthy and falsy values-

Falsy: 1. 0 as a number
2. empty string (‘’)
3. null
4. undefined
5. NaN
6. false

Truthy: All the other values that are not falsy , are the truthy values.

2. Null vs undefined

Ans: In JavaScript , undefined is a type but null is an object. When a variable is declared but no value has been assigned there then we can call it
undefined. But when it comes to the term null, we have to assign null as value. Null means nothing in there. Both undefined and null are the falsy
values in JavaScript.

3. == vs ===

Ans: “==” checks only the value is same or not. If it is same then returns true.
“===” check both the value and the data type. If both are same then returns true , else false.

Ex-1: let age1 = “22”;
if(age1 == 22){
console.log(“equal”)
}else{
console.log(“not equal”)
} // here == checks only the value 22. but there type is different. one is string, the other is number. But the output is equal.

Ex-2: let age2 = “22”;
if(age2 === 22){
console.log(“equal”)
}else{
console.log(“not equal”)
} // here == checks both the value 22 and the data type. but there type is different. one is string, the other is number. So the output is not equal.

4. scope

Ans: scope means a context of the code which determines the accessibilities of variables to JavaScript. In simple word, I will say scope is a separate
world of the code. There are two type of scopes. Local and Global. If we declare a variable in its global scope. then it can be accessible from anywhere
but it we declare a variable in a local scope then it can be accessible from its own function scope. but if we declare a variable with let in a global
scope then it can be accessible from only its block scope. Let’s see the example-

var x = 10;

function test(){
var y = 20;
console.log(x) //(output 10) //it can access its parent variable for the scope.
console.log(y) //(output 20)
}

console.log(x) //(output 10)
console.log(y) //(output -error) // it can not be accessible outside of it’s block scope because it’s a local scope.

5. Window object

Ans: Window object represents the browser’s window. All global JavaScript objects, functions, and variables automatically become members of the window object.
Global variables are properties of the window object. Global functions are methods of the window object.

6. Global variable

Ans: In JavaScript, Global variable means a variable which is defined outside of a function or it can be said that a global variable is declared with
window object. It’s scope is global. That’s why it can be accessible from anywhere.

Ex: let x = 100;

if(x>0){
x++;

}

console.log(x); //output 101; x has increased inside the if block. x is global variable and accessible from anywhere.

7. Global scope

Ans: In JavaScript, Global scope is the complete JavaScript environment. In HTML, the global scope is the window object. All global variables belong to the
window object. Global variables can be accessed and modified anywhere in the program because of the global scope. The previous example can be used
here also.

8. this

Ans: The JavaScript this keyword refers to the object it belongs to. In other words, every JavaScript function while executing has a reference to its
current execution context called this.

9. Synchronous vs Asynchronous

Ans: Synchronous code is executed sequentially. we know when our code is executed JavaScript execute the code line by line. When one task is finished
then it will go for the next task. The next code had to wait for the completion of the previous one. But Asynchronous code allows the program to
be executed immediately. Like, setTimeout is a example of asynchronous JavaScript-

Ex: console.log(“hello”);

setTimeout( ()=>{

console.log(“After 3s i will appear”);

},3000);

console.log(“hi”);

Here , for the asynchronous rule first the code will execute “hello”, then it won’t wait for 3s for the setTimeout. It will execute the next code and
show to the console “hi”. then after 3s have finished it will execute the code and show to the console “After 3s i will appear”.

10. Event Loop

Ans: The Event Loop has one simple job that is to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, the Event Loop will take the
first event from the queue and will push it to the Call Stack. This iteration is called the Event Loop.

--

--

Jahid Hasan Siam
Jahid Hasan Siam

Written by Jahid Hasan Siam

0 Followers

Hello, I am Jahid Hasan Siam. A full Stack Web Developer. I have been learning web development for last 2 years.

No responses yet