JavaScript Interview Questions Part 1 🚀🔥

Arjun Vijay Prakash - Jun 13 '22 - - Dev Community

Wouldn’t it be great if you knew exactly what questions a hiring manager would be asking you in your next job interview?
I have got you covered I will be telling the answers to the frequently asked JavaScript Question.
Till Then, Stay Tuned.

1) What is Hoisting in JavaScript?

Hoisting is the default behaviour of JavaScript where all the variable and function declarations are moved on top.
This means that irrespective of where the variables and functions are declared, they are moved on top of the scope. The scope can be both local and global.
Example-

// Example of Global Scope
hoistedVariable = 3;
console.log(hoistedVariable); // outputs 3 even when the variable is declared after it is initialized   
var hoistedVariable;

// --------------------

// Example of Local Scope
function doSomething(){
  x = 33;
  console.log(x); // Outputs 33 since the local variable “x” is hoisted inside the local scope
  var x;
} 
Enter fullscreen mode Exit fullscreen mode

2) Explain the difference between let and var keywords in JavaScript.

In JavaScript, both the keywords var and let are used to declare variables.
The let keyword was introduced in the later version of JavaScript known as ES6(ES2015). And it's the preferred way to declare variables.
The main differences are-

  • let is block-scope.
  • var is function scoped.
  • let does not allow to redeclare variables.
  • var allows to redeclare variables.
  • Hoisting does not occur in let.
  • Hoisting occurs in var. Example-
// -----var example-----
// program to print text
// variable a cannot be used here
function greet() {
    // variable a can be used here
    var a = 'hello';
    console.log(a);
}
// variable a cannot be used here

greet(); // hello

// -----let example-----
// program to print the text
// variable a cannot be used here
function greet() {
    let a = 'hello';

    // variable b cannot be used here
    if(a == 'hello'){
        // variable b can be used here
        let b = 'world';
        console.log(a + ' ' + b);
    }

     // variable b cannot be used here
    console.log(a + ' ' + b); // error
}
// variable a cannot be used here

greet();

Enter fullscreen mode Exit fullscreen mode

3) Difference between “ == “ and “ === “ operators.

Both are comparison operators. The difference between both the operators is that “==” is used to compare values whereas, “ === “ is used to compare both values and types.
Example-


var x = 2;

var y = "2";

(x == y)  // Returns true since the value of both x and y is the same

(x === y) // Returns false since the typeof x is "number" and typeof y is "string"

Enter fullscreen mode Exit fullscreen mode

4) What is recursion in a programming language?

Recursion is a technique to iterate over an operation by having a function call itself repeatedly until it arrives at a result.
Example-

function add(number) {
  if (number <= 0) {
    return 0;
  } else {
    return number + add(number - 1);
  }
}
/* add(3) => 3 + add(2)
          3 + 2 + add(1)
          3 + 2 + 1 + add(0)
          3 + 2 + 1 + 0 = 6 */
Enter fullscreen mode Exit fullscreen mode

5) What are arrow functions in JavaScript?

Arrow functions were introduced in the ES6 version of javascript. They provide us with a new and shorter syntax for declaring functions. Arrow functions can only be used as a function expression.
Syntax-

const add = (num) => num * 2;
// ---or---
const add = (num) => {
num * 2;
};
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .