Being loosely typed programming language, javascript never fails to surprise.
It some times get so tricky that even experienced professionals get confused.
In this set we have curated list of javascript questions and answers which are differentiated topic wise. Each question will have its topic’s tag and difficulty label.
1. Which element will be fetched from the array?
//What will be the output of the following code? const arrTest = [10, 20, 30, 40, 50][1, 3]; console.log(arrTest);
Options
Answer
40
The last element from the second array is used as the index to get the
value from first array like arrTest[3];
2. Which value will the array hold?
//What will be the output of the following code? let arr = [1, 2, 3, 4]; arr.pop(); arr.length = 0; arr.push(0); console.log(arr);
Options
Answer
[0]
The array becomes empty when arr.length = 0
, after that we
push 0 in it arr.push(0)
.
3. What will happen when the below code is executed?
"use strict" function setName() { this.name = 'learnersbucket'; } setName(); console.log(this.name);
Options
Answer
An error will be thrown
In strict mode if the value of this
is not while entering execution context it will remain undefined
, As we cannot set property on undefined
error is thrown.
4. What will be the value of this.name
?.
function setName() { this.name = 'learnersbucket'; } setName(); console.log(this.name);
Options
Answer
‘learnersbucket’
The value of this
inside the function is pointing to the global object window
, hence once its property is updated it is accessible outside as well.
5. What will be the outcome of square(5)
?.
console.log(square(5)); function square(n) { return n * n }
Options
Answer
25
functions defined with base syntax function functionName() {}
can be called before defining them.
6. What will be the result when this function is called before it is defined?.
console.log(square(5)); const square = function(n) { return n * n }
Options
Answer
An error will be thrown
As we are assigning the function to the variable, the variable const square
will be hoisted on the top and it’s value is undefined
hence invoking it like a function throws an error Uncaught TypeError: square is not a function
.
7. What will be the result after invoking function like this?.
function A(x) { function B(y) { function C(z) { console.log(x + y + z); } C(3); } B(2); } A(1);
Options
Answer
6
An inner function can access the values of its parent function.
8. What value will this function return?.
function outside() { let x = 5; function inside(x) { return x * 2; } return inside; } outside()(10);
Options
Answer
20
If there are variable with same name then one in the current scope are given more preference.
9. What value will this function print?.
function a() { this.site = 'Learnersbucket'; function b(){ console.log(this.site); } b(); } var site = 'Wikipedia'; a();
Options
Answer
‘Learnresbucket’
When a function with normal syntax is executed the value of this
of the nearest parent will be used if not set at execution time, so in this case it is window
, we are then update setting the property site
in the window
object and accessing it inside so it is 'Learnersbucket'
.
10. What value will this function print?.
function a() { this.site = 'Learnersbucket'; function b(){ console.log(this.site); } b(); } var site = 'Wikipedia'; new a();
Options
Answer
‘Wikipedia’
When a function is invoked as a constructor with new
keyword then the value of this
will be new object which will be created at execution time so currently it is { site: 'Learnersbucket' }
.
But when a function with normal syntax is executed the value of this
will be default to global scope if it is not assigned at the execution time so it is window
for b()
and var site = 'Wikipedia';
adds the value to the global object, hence when it is accessed inside b()
it prints 'Wikipedia'
.
11. Why this function prints undefined
?.
function a() { this.site = 'Learnersbucket'; function b(){ console.log(this.site); } b(); } let site = 'Wikipedia'; new a();
Options
Answer
Variables defined with let
are not added to global scope.
let
defines blocked scope variables and they are not added to global object.
12. What will be the result of following code?.
'use strict'; { function a() { console.log('Hello'); } } a();
Options
Answer
An error will be thrown
Starting from ES2015 functions in strict mode are blocked scoped thus it throws error 'a is not defined'
.
13. What will this function return?.
function a() { 'use strict'; // see strict mode return this; } console.log(a());
Options
Answer
undefined
In strict mode if the value of this
is not set at the execution time then it is undefined.
14. What will this function return when called as constructor?.
function a() { 'use strict'; // see strict mode return this; } console.log(new a());
Options
Answer
{}
Every time a function is called as constructor the value of this
inside it is set as empty object.
15. What is the right way of defining function in javascript?.
//1 const multiply = new Function('x', 'y', 'return x * y'); //2 function multiply(x, y) { return x * y; } //3 const multiply = function(x, y) { return x * y; }; //4 const multiply = function func_name(x, y) { return x * y; }; //5 const multiply = (x, y) => { return x * y; }
Options
Answer
1, 2, 3, 4, 5.
All of them are the right way to define the functions.
1. Function is defined as constructor.
2. Function is defined as declaration.
3, 4. Function is defined as expression and assigned to variable.
5. Fat arrow function introduced in ES2015.
16. What does this program validates?.
const validate = (inp) => { try{ JSON.parse(inp); }catch (e) { return false; } return true; }
Options
Answer
It checks if the string is a valid JSON.
JSON.parse
is used to parse a string to JSON. wrapping it inside a try catch block helps to check if the string is a valid JSON or not.
17. What will be the output of the following code?.
const site = {}; site.learnersbucket = null; const learnersbucket = sites.hasOwnProperty('learnersbucket'); console.log(learnersbucket);
Options
Answer
true.
hasOwnProperty
checks if the object has this property of its own and not in its prototype chain and returns true
if it exists, false
otherwise.
18. What will be the output of the following code?.
const ans1 = NaN === NaN; const ans2 = Object.is(NaN, NaN); console.log(ans1, ans2);
Options
Answer
false true.
NaN
is a unique value so it fails in equality check, but it is the same object so Object.is
returns true
19. What will be the output of the following code?.
let lang = 'javascript'; (function(){ let lang = 'java'; })(); console.log(lang);
Options
Answer
javascript.
variables defined with let
are blocked scope and are not added to global object.
20. What will be the output of the following code?.
(function(){ var lang = 'java'; })(); console.log(lang);
Options
Answer
Error will be thrown.
Variables declared with var
keyword are function scoped, so wrapping the function inside a closure will restrict it being accessed outside that is why it throws error.
21. What will be the output of the following code?.
function sum(){ return arguments.reduce((a, b) => a + b); } console.log(sum(1,2,3));
Options
Answer
Error will be thrown.
Arguments are not full functional array, they have only one method length
. Other methods cannot be used on them.
22. What will be the output of the following code?.
function sum(...arguments){ return arguments.reduce((a, b) => a + b); } console.log(sum(1,2,3));
Options
Answer
6.
...
rest operator creates an array of all functions parameter. We then use this to return the sum of them.
23. What will be the output of the following code?.
const bestBlog = { site: 'learnersbucket' } const { site: webSite } = bestBlog; console.log(webSite);
Options
Answer
‘learnersbucket’.
From ES6 we can pull object keys using destructuring and and assign to the new variable.
24. What will be the output of the following code?.
(function(){ console.log(typeof this); }).call(10);
Options
Answer
object.
call
invokes the function with new this
which in this case is 10
which is basically a constructor of Number
and Number
is object in javascript.
25. What will be the output of the following code?.
const set = new Set(); const blog = { site: 'learnersbucket'}; set.add(blog); set.add(blog); console.log(set.size);
Options
Answer
1.
Set stores the unique objects in itself and we are storing same object twice that is why it is size is 1.
26. What will be the output of the following code?.
console.log("Learnersbucket.com" instanceof String);
Options
Answer
false.
Only strings defined with String()
constructor are instance of it.
27. What will be the output of the following code?.
const s = new String('learnersbucket.com'); console.log(s instanceof String);
Options
Answer
true.
Strings defined with String()
constructor are instance of it.
28. What will be the output of the following code?.
console.log("learnersbucket.com" === `learnersbucket.com`);
Options
29. What will be the output of the following code?.
console.log(new Array(3).toString());
Options
Answer
“,,”.
Array.toString()
creates string of the array with comma separated values.
30. What will be the output of the following code?.
console.log([] + []);
Options
Answer
“”.
An empty array is while printing in console.log is treated as Array.toString(), so it prints empty string.
31. What will be the output of the following code?.
console.log([1] + []);
Options
Answer
“1”.
An empty array when printed in console.log is treated as Array.toString()
and so it is basically “1” + “”
= ""
.
32. What will be the output of the following code?.
console.log([1] + "abc");
Options
Answer
“1abc”.
An empty array when printed in console.log is treated as Array.toString()
and so it is basically “1” + “abc”
= "1abc"
.
33. What will be the output of the following code?.
console.log([1, 2, 3] + [1, 3, 4]);
Options
Answer
“1,2,31,3,4”.
An empty array when printed in console.log is treated as Array.toString()
and so it is basically “1, 2, 3” + “1, 3, 4”
= "1,2,31,3,4"
.
34. What will be the output of the following code?.
console.log('1' - - '1')
Options
Answer
2.
With type coercion string is converted to number and are treated as 1 - -1
= 2
.
35. What will be the output of the following code?.
console.log('1' + - '1')
Options
Answer
“1-1”.
+ operator is used for concatenation of strings in javascript, so it is evaluated as '1' + '-1'
= 1-1
.
36. What does this code do?.
const solution = (a, b) => { while(b != 0){ let carry = a & b; a = a ^ b; b = carry << 1; } return a; }
Options
Answer
Performs addition of two numbers.
This function adds two numbers without using any arithmetic operators.
37. What will be the output of the following code?.
var a = 3; var b = { a: 9, b: ++a }; console.log(a + b.a + ++b.b);
Options
Answer
18.
Prefix operator increments the number and then returns it. So the following expression will be evaluated as 4 + 9 + 5
= 18
.
38. What will be the output of the following code?.
console.log(1 == '1'); console.log(false == '0'); console.log(true == '1'); console.log('1' == '01'); console.log(10 == 5 + 5);
Options
Answer
true true true false true.
'1' == '01'
as we are comparing two strings here they are different but all other equal.
39. What will be the output of the following code?.
let a = 10, b = 20; setTimeout(function () { console.log('learnersbucket'); a++; b++; console.log(a + b); }); console.log(a + b);
Options
Answer
30 "learnersbucket" 32.
Settimeout pushes the function into BOM stack in event loop or it is executed after everything is executed in main function. So the results are printed after the console.log of main function.
40. What will be the output of the following code?.
const arr = [1, 2, 3, 4, 5]; arr.unshift(6); arr.shift(); arr.unshift(7); console.log(arr);
Options
Answer
[7, 1, 2, 3, 4, 5].
unshift()
adds the item at the front of the array and shift()
removes the item from the front.
41. What will be the output of the following code?.
const a = {}; const b = {key: "b"}; const c = {key: "c"}; a[b] = 123; a[c] = 456; console.log(a[b]);
Options
Answer
456.
b
and c
are objects so when we use them as a key in another object they are converted to a string because of getting called with toString()
method. So in a
the key becomes for b
= a['[object object]'] = 123
and for c
= a['[object object]'] = 456
thus it overwrites the key '[object object]'
. That is why when we access it, it returns 456.
42. What will be the output of the following code?.
let a = 2; let b = 7; console.log(a + (--b + a) + b++);
Options
Answer
16.
Prefix operator increments the number and returns the incremented values where as Postfix increments the number but returns the original value.
43. What will be the output of the following code?.
function test() { return 1000; } console.log(typeof test());
Options
Answer
"number".
Function returned a number, so it is of "number" type.
44. What will be the output of the following code?.
let a = 5; let b = a ^ 3 * 3; console.log(b);
Options
Answer
12.
It is evaluated as 5 ^ 9
= 12
.
45. What will be the output of the following code?.
const arr = [1, 2, undefined, NaN, null, false, true, "", 'abc', 3]; console.log(arr.filter(Boolean));
Options
Answer
[1, 2, true, "abc", 3].
Array.filter()
returns the array which matches the condition. As we have passed Boolean it returned all the truthy value.
46. What will be the output of the following code?.
const arr = [1, 2, undefined, NaN, null, false, true, "", 'abc', 3]; console.log(arr.filter(!Boolean));
Options
Answer
It will throw an error.
As Array.filter()
accepts a function, !Boolean
returns false
which is not a function so it throws an error Uncaught TypeError: false is not a function
.
47. What will be the output of the following code?.
const person = { name: 'Prashant Yadav', .25e2: 25 }; console.log(person[25]); console.log(person[.25e2]); console.log(person['.25e2']);
Options
Answer
25 25 undefined.
While assign the key the object evaluates the numerical expression so it becomes person[.25e2] = person[25]
. Thus while accessing when we use 25
and .25e2
it returns the value but for '.25e2'
is undefined
.