Backbencher.dev

JavaScript Questions

Last updated on 7 Feb, 2021

Here is a set of questions in JavaScript. It can test our JavaScript skills and teach a lot of new things.

Question #1:

What is the output printed in the console?

var a = 4;

function baaz() {
  a = 6;
  boom();
}

function boom() {
  var a = 8;
}

baaz();
console.log(a);

Question #2:

What is a closure?

Question #3:

Here is a code snippet:

setTimeout(function () {
  console.log("Backbencher");
}, 2000);

Above code, does it form a closure?

Question #4:

Here is a JavaScript code snippet.

var name = obj.name;
var age = obj.age !== undefined ? obj.age : 0;

How can we refactor above code using JavaScript destructuring?

Question #5:

We have an array and elements from the array are assigned to variables.

var arr = ["B", "a", "c", "k"];
var first = arr[0];
var second = arr[1];

Refactor this code using destructuring.

Question #6:

What is the output of following code snippet?

var arr = ["B", , "c", "k"];
var [first, second] = arr;

console.log(second);

Question #7:

What is the output of following code snippet?

var arr = ["B", null, "c", "k"];
var [first, second] = arr;

console.log(second);

Question #8:

What is the output of following code snippet?

var arr = ["B", , "c", "k"];
var [first, second = 10] = arr;

console.log(second);

Question #9:

What is the output of following code snippet?

var arr = ["B", null, "c", "k"];
var [first, second = 10] = arr;

console.log(second);

Question #10:

How can we refactor below code using destructuring?

var arr = ["B", "a", "c", "k", "b", "e", "n", "c", "h", "e", "r"];
var first = arr[0];
var second = arr[1];
var rest = arr.slice(2);

Question #11:

What is the output of following code snippet?

var arr = ["B", "a"];
var [first, second, ...rest] = arr;

console.log(rest);

Question #12:

Is this code valid?

var arr = ["B", "a", "c", "k"];
var first, second, third;
[first, second, third] = arr;

Question #13:

Here is a JavaScript code snippet.

var arr = ["Joe", 23, "Male"];
var profile = {};
profile.name = arr[0];
profile.age = arr[1];
profile.gender = arr[2];
console.log(profile);

Refactor above code using destructuring.

Question #14:

Here we have a code that copies first two elements of an array to another array.

var arr = ["Joe", 23, "Male"];
var newArr = [];
newArr[0] = arr[0];
newArr[1] = arr[1];
console.log(newArr);

Refactor it using destructuring.

Question #15:

Here is an array.

var arr = ["B", "a", "c", "k"];

How to pick only "B" and "k" from arr using destructuring?

Question #16:

Here is a JavaScript code snippet to swap two numbers.

var a = 2;
var b = 5;
var temp = a;
a = b;
b = temp;
console.log(a);
console.log(b);

Refactor the code without using temp variable.

Question #17:

We have a function that extracts values from its parameter array.

function sum(numbers) {
  var num1 = numbers[0];
  var num2 = numbers[1];
  return num1 + num2;
}

Refactor the code using destructuring.

Question #18:

Write the ES5 equivalent of following code.

function sum([num1 = 1, num2 = 1] = []) {
  return num1 + num2;
}

Question #19:

How can we refactor the below code using destructuring?

var arr = [1, 2, [3, 4]];
var a = arr[0];
var b = arr[1];
var c = arr[2][0];
var d = arr[2][1];

console.log(a, b, c, d);

Question #20:

Refactor below code using destructuring.

var obj = {
  name: "Backbencher",
  age: 23,
  planet: "Earth",
};

var objName = obj.name;
var objAge = obj.age;
var objPlanet = obj.planet;

console.log(objName, objAge, objPlanet);

Question #21:

Here we have an object and 3 variables.

var obj = {
  a: "Apple",
  b: "Boy",
  c: "Cat",
  d: "Dog",
  e: "Elephant",
};

We want obj.a to be assigned to a variable first, obj.b to be assigned to a variable second and rest of the object assigned to a variable rest. Write the code for it.

Question #22:

Here we have an object destructuring code that looks perfectly fine.

var obj = {
  a: "Apple",
  b: "Boy",
  c: "Cat",
  d: "Dog",
  e: "Elephant"
}

var first, second;

{
  a: first,
  b: second,
} = obj;

console.log(first);
console.log(second);

But when executed, it throws an error saying:

SyntaxError: Unexpected token ':'

What is the problem? And how can it be fixed?

Question #23:

We have an ES5 code to assign object properties to variables.

var obj = {
  fruit: "Apple",
  vegetable: "carrot",
};

var fruit = obj.fruit;
var vegetable = obj.vegetable;

console.log(fruit);
console.log(vegetable);

Refactor above code using destructuring.

Question #24:

Here we have an object nested inside another object.

var profile = {
  name: "Backbencher",
  age: 34,
  address: {
    state: "Kerala",
    country: "India",
    pincode: 682037,
  },
};

var name = profile.name;
var state = profile.address.state;
var country = profile.address.country;

console.log(name);
console.log(state);
console.log(country);

Refactor this code using destructuring.

Question #25:

What is the value printed in the console?

var obj = {
  a: {
    b: 10,
  },
};

var {
  a: { b, c } = {
    b: 10,
    c: 15,
  },
} = obj;

console.log(c);

Question #26:

What is the output printed?

var obj = {
  username: "backbencher",
  social: {
    likes: 245,
    tweets: 42,
    shares: 600,
  },
};

var {
  username,
  social,
  social: { likes, tweets },
} = obj;

console.log(social); // 1
console.log(likes); // 2
console.log(tweets); // 3

Question #27:

How can we bring named argument support to JavaScript using destructuring?

Question #28:

What is async-await in JavaScript?

Question #29:

We all have heard that JavaScript is an interpreted language? Then why below code does not print "Backbencher" in the console. Instead, the code is throwing a syntax error.

var name = "Backbencher";

console.log(name);

name = "Hi"+;

Question #30:

What is the output of following code snippet?

console.log("Backbencher");

baz("Hello","World");

function baz(name, name) {
    "use strict";
    console.log(name);
}

Question #31:

What will be the output when following code is run?

var a = 10;
{
  console.log(a);
  let a = 20;
}

Explain how that answer came.

Question #32:

What is the output?

for (var i = 1; i <= 3; i++) {
  setTimeout(() => console.log(i), 1000);
}
--- ○ ---
Joby Joseph
Web Architect