JavaScript some important question and answer

MD Moshiur Rahman
5 min readMay 9, 2021
  1. What is JavaScript truthy and falsely value??
  • Any number is negative or positive value , any string, array or object is truthy value. But 0 (zero), empty string (“”), variable value is null, NaN, false and declare variable but not assign value are return falsy value.

Example

example:1
let price = 60;
or let price = "0";
or let price = " ";
if (price){
console.log(true)
}else {
console.log(false)
}
//output : true
example:2
let price = 0;
or, let price = "";
or, let price = null;
or, let price;
or, let price = NaN;
or, let price = false;
if (price){
console.log(true)
}else {
console.log(false)
}
//output : false

2. Null Vs Undefined, different ways you will get undefined

  • Many way to has undefined result. First way is a variable declare but no assign value. Second way is function parameter declare but not return value then this function default value return undefined. In array and object value not defiant but try this value access then this value result undefined . But Null is not a default value. This value set then this result null.

Example

let name;
console.log(name)
//output: undefined
function add(num1, num2){
return
}
add(10,20);
//output: undefined
let number = null;
console.log(number)
//output: null

3.What is deferent double equal (==) and triple equal (===) ??

  • Double equal and triple equal main deferent is double equal check only value but triple equal check value and data type. Double equal first check value and type, if type deferent then try convert same type and give result true or false. Triple equal check value and data type, if value or type is deferent then result falsy.

Example

let num1 = 4;
let num2 = "4";
if(num1 == num2){
console.log("This is true")
}
else{
console.log("This is false")
}
//output : This is true
if (num1 === num2){
console.log("This is true")
}
else{
console.log("This is false")
}
//output : This is false

4. What is Scope, block scope, access outer scope or hosting variable??

  • Any variable declare let or const in function or condition this variable work only in function or condition. If want to this variable access out side in function or condition then this variable result undefined. Any variable declare in scope var keyword then this variable declaration part host her upper level parent variable, and this variable access out side in function or condition. Again if variable declare in parent level function or condition then this variable accessible any child place, call this global scope variable.

Example

let bonus = 60;
function add(num1, num2){
var num3 = 50;
let result = num1 + num2 + nam3 + bonus;
return result;
}
const sum = add(6, 4);
console.log("sum =" sum);
console.log("num3 =",num3);
console.log ("bonus =" bonus)
console.log(result);
//output sum = 120, num3 = 50, bonus =60, result is not defined

5.What is Closure in JavaScript??

  • If any function in stays second other function. This second function call or return or use in function, then its make a closed environment . And this function keep own external variable referends. This is called general closure.

Example

function countNumber(){
let countNumber = 0;
return function(){
countNumber++;
return countNumber;
}
}
const counter1 = countNumber()
console.log(conuter1())
console.log(conuter1())
console.log(conuter1())
const counter2 = countNumber()
console.log(conuter2())
console.log(conuter2())
console.log(conuter1())
//output
1
2
3
1
2
4

6.What is difference between bind, call and apply??

  • Bind : If one method want to use repeat then with that method you have to bind with the new object. As needed to call this.
  • Call : call is being that method calls first argument as you have to pass the object on which you want to apply, that parameters send with coma separation.
  • Apply : apply method work almost same call method. This method of another object that needs to be used will be applied with this as a fast argument. But the parameters of the method that will be applied have to be sent inside an array.

7.What is DOM (Document Object Model) ??

  • Document Object Models (DOM) are used to represent HTML and XML documents, allowing programs to alter the structure, style and content as well as formatting them. DOM represents web pages in an object-oriented manner in accordance with the Java Script programming language.

8.how to understand the this keyword??

  • “this” refers to a new Instance. When a function is invoked with the new keyword, then the function is known as a constructor function and returns a new instance. “this” keyword with specific access one property in object.

9.How JavaScript works event loop stack and queue??

  • Stack : A stack normally is a structure of sequential and ordered elements and it’s based on the principle of last in first out (LIFO)

A stack data structure has two fundamental operations:

i. push — This operation is responsible for inserting or pushing a new element to the stack.

ii. pop — This operation is responsible for removing the most recent element from the stack.

  • Queue : A queue is a linear structure of sequential and ordered elements, similar to a stack, with a difference that it works based on the principle of first in first out (FIFO).

A queue data structure has two fundamental operations:

  1. push — This operation is responsible for inserting a new element to the queue.
  2. pop — This operation is responsible for removing the oldest element from the queue.

10. What is Arrow function, multiple parameter, function body ??

  • An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations. This function when need multiple parameter then write multiple parameter. And write function body.

--

--

MD Moshiur Rahman
0 Followers

Frontend Web Developer || React JS