JavaScript Interview Questions and Answers from FullStack.Cafe
You could also find all the answers here π https://www.fullstack.cafe/JavaScript.
Answer:
In JavaScript conversion between different two build-in types called coercion
. Coercion comes in two forms in JavaScript: explicit and implicit.
Here's an example of explicit coercion:
var a = "42";
var b = Number( a );
a; // "42"
b; // 42 -- the number!
And here's an example of implicit coercion:
var a = "42";
var b = a * 1; // "42" implicitly coerced to 42 here
a; // "42"
b; // 42 -- the number!
π Source: FullStack.Cafe
Answer:
In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function's scoped variables.
A variable name has to be unique within the same scope. A scope can be nested inside another scope. If one scope is nested inside another, code inside the innermost scope can access variables from either scope.
π Source: FullStack.Cafe
Answer:
JavaScript has both strict and typeβconverting comparisons:
- Strict comparison (e.g., ===) checks for value equality without allowing coercion
- Abstract comparison (e.g. ==) checks for value equality with coercion allowed
var a = "42";
var b = 42;
a == b; // true
a === b; // false
Some simple equalityrules:
- If either value (aka side) in a comparison could be the
true
orfalse
value, avoid==
and use===
. - If either value in a comparison could be of these specific values (
0
,""
, or[]
-- empty array), avoid==
and use===
. - In all other cases, you're safe to use
==
. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.
π Source: FullStack.Cafe
Answer:
JavaScript provides a typeof
operator that can examine a value and tell you what type it is:
var a;
typeof a; // "undefined"
a = "hello world";
typeof a; // "string"
a = 42;
typeof a; // "number"
a = true;
typeof a; // "boolean"
a = null;
typeof a; // "object" -- weird, bug
a = undefined;
typeof a; // "undefined"
a = { b: "c" };
typeof a; // "object"
π Source: FullStack.Cafe
Answer:
The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type.
var obj = {
a: "hello world", // property
b: 42,
c: true
};
obj.a; // "hello world", accessed with doted notation
obj.b; // 42
obj.c; // true
obj["a"]; // "hello world", accessed with bracket notation
obj["b"]; // 42
obj["c"]; // true
Bracket notation is also useful if you want to access a property/key but the name is stored in another variable, such as:
var obj = {
a: "hello world",
b: 42
};
var b = "a";
obj[b]; // "hello world"
obj["b"]; // 42
π Source: FullStack.Cafe
Answer:
An array
is an object that holds values (of any type) not particularly in named properties/keys, but rather in numerically indexed positions:
var arr = [
"hello world",
42,
true
];
arr[0]; // "hello world"
arr[1]; // 42
arr[2]; // true
arr.length; // 3
typeof arr; // "object"
π Source: FullStack.Cafe
Answer:
JavaScript has typed values, not typed variables. The following built-in types are available:
string
number
boolean
null
andundefined
object
symbol
(new to ES6)
π Source: FullStack.Cafe
Answer:
JavaScript (and by extension TypeScript) has two bottom types: null
and undefined
. They are intended to mean different things:
- Something hasn't been initialized :
undefined
. - Something is currently unavailable:
null
.
π Source: FullStack.Cafe
Answer:
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
// Non-strict code...
(function(){
"use strict";
// Define your library strictly...
})();
// Non-strict code...
π Source: FullStack.Cafe
Answer:
A polyfill is essentially the specific code (or plugin) that would allow you to have some specific functionality that you expect in current or βmodernβ browsers to also work in other browsers that do not have the support for that functionality built in.
- Polyfills are not part of the HTML5 standard
- Polyfilling is not limited to Javascript
π Source: programmerinterview.com
Answer:
In addition to creating declarations for variables at the function level, ES6 lets you declare variables to belong to individual blocks (pairs of { .. }), using the let
keyword.
π Source: github.com/getify
Q12: Being told that an unsorted array contains (n - 1) of n consecutive numbers (where the bounds are defined), find the missing number in O(n) time ββ
Answer:
// The output of the function should be 8
var arrayOfIntegers = [2, 5, 1, 4, 9, 6, 3, 7];
var upperBound = 9;
var lowerBound = 1;
findMissingNumber(arrayOfIntegers, upperBound, lowerBound); // 8
function findMissingNumber(arrayOfIntegers, upperBound, lowerBound) {
// Iterate through array to find the sum of the numbers
var sumOfIntegers = 0;
for (var i = 0; i < arrayOfIntegers.length; i++) {
sumOfIntegers += arrayOfIntegers[i];
}
// Find theoretical sum of the consecutive numbers using a variation of Gauss Sum.
// Formula: [(N * (N + 1)) / 2] - [(M * (M - 1)) / 2];
// N is the upper bound and M is the lower bound
upperLimitSum = (upperBound * (upperBound + 1)) / 2;
lowerLimitSum = (lowerBound * (lowerBound - 1)) / 2;
theoreticalSum = upperLimitSum - lowerLimitSum;
return theoreticalSum - sumOfIntegers;
}
π Source: https://github.com/kennymkchan
Answer:
// ES6 Implementation
var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8];
Array.from(new Set(array)); // [1, 2, 3, 5, 9, 8]
// ES5 Implementation
var array = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8];
uniqueArray(array); // [1, 2, 3, 5, 9, 8]
function uniqueArray(array) {
var hashmap = {};
var unique = [];
for(var i = 0; i < array.length; i++) {
// If key returns undefined (unique), it is evaluated as false.
if(!hashmap.hasOwnProperty(array[i])) {
hashmap[array[i]] = 1;
unique.push(array[i]);
}
}
return unique;
}
π Source: https://github.com/kennymkchan
Questions Details:
For example Welcome to this Javascript Guide!
should be become emocleW ot siht tpircsavaJ !ediuG
Answer:
var string = "Welcome to this Javascript Guide!";
// Output becomes !ediuG tpircsavaJ siht ot emocleW
var reverseEntireSentence = reverseBySeparator(string, "");
// Output becomes emocleW ot siht tpircsavaJ !ediuG
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");
function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}
π Source: https://github.com/kennymkchan
Answer:
Enqueue means to add an element, dequeue to remove an element.
var inputStack = []; // First stack
var outputStack = []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while(stackInput.length > 0) {
var elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}
π Source: https://github.com/kennymkchan
Answer:
Event bubbling is the concept in which an event triggers at the deepest possible element, and triggers on parent elements in nesting order. As a result, when clicking on a child element one may exhibit the handler of the parent activating.
One way to prevent event bubbling is using event.stopPropagation()
or event.cancelBubble
on IE < 9.
π Source: https://github.com/kennymkchan
Questions Details:
console.log(mul(2)(3)(4)); // output : 24
console.log(mul(4)(3)(4)); // output : 48
Answer:
function mul (x) {
return function (y) { // anonymous function
return function (z) { // anonymous function
return x * y * z;
};
};
}
Here mul
function accept the first argument and return anonymous function which take the second parameter and return anonymous function which take the third parameter and return multiplication of arguments which is being passed in successive
In JavaScript function defined inside has access to outer function variable and function is the first class object so it can be returned by function as well and passed as argument in another function.
- A function is an instance of the Object type
- A function can have properties and has a link back to its constructor method
- Function can be stored as variable
- Function can be pass as a parameter to another function
- Function can be returned from function
π Source: github.com/ganqqwerty
Questions Details:
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f'];
How could we empty the array above?
Answer:
Method 1
arrayList = [];
Above code will set the variable arrayList
to a new empty array. This is recommended if you don't have references to the original array arrayList
anywhere else because It will actually create a new empty array. You should be careful with this way of empty the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged, Only use this way if you have only referenced the array by its original variable arrayList
.
For Instance:
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList; // Referenced arrayList by another variable
arrayList = []; // Empty the array
console.log(anotherArrayList); // Output ['a', 'b', 'c', 'd', 'e', 'f']
Method 2
arrayList.length = 0;
Above code will clear the existing array by setting its length to 0. This way of empty the array also update all the reference variable which pointing to the original array. This way of empty the array is useful when you want to update all the another reference variable which pointing to arrayList
.
For Instance:
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList; // Referenced arrayList by another variable
arrayList.length = 0; // Empty the array by setting length to 0
console.log(anotherArrayList); // Output []
Method 3
arrayList.splice(0, arrayList.length);
Above implementation will also work perfectly. This way of empty the array will also update all the references of the original array.
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList; // Referenced arrayList by another variable
arrayList.splice(0, arrayList.length); // Empty the array by setting length to 0
console.log(anotherArrayList); // Output []
Method 4
while(arrayList.length) {
arrayList.pop();
}
Above implementation can also empty the array. But not recommended to use often.
π Source: github.com/ganqqwerty
Answer:
The best way to find whether an object is instance of a particular class or not using
toString
method fromObject.prototype
var arrayList = [1 , 2, 3];
One of the best use cases of type checking of an object is when we do method overloading in JavaScript. For understanding this let say we have a method called greet
which take one single string and also a list of string, so making our greet
method workable in both situation we need to know what kind of parameter is being passed, is it single value or list of value?
function greet(param) {
if() {
// here have to check whether param is array or not
}
else {
}
}
However, in above implementation it might not necessary to check type for array, we can check for single value string and put array logic code in else block, let see below code for the same.
function greet(param) {
if(typeof param === 'string') {
}
else {
// If param is of type array then this block of code would execute
}
}
Now it's fine we can go with above two implementations, but when we have a situation like a parameter can be single value
, array
, and object
type then we will be in trouble.
Coming back to checking type of object, As we mentioned that we can use Object.prototype.toString
if(Object.prototype.toString.call(arrayList) === '[object Array]') {
console.log('Array!');
}
If you are using jQuery
then you can also used jQuery isArray
method:
if($.isArray(arrayList)) {
console.log('Array');
} else {
console.log('Not an array');
}
FYI jQuery uses Object.prototype.toString.call
internally to check whether an object is an array or not.
In modern browser, you can also use:
Array.isArray(arrayList);
Array.isArray
is supported by Chrome 5, Firefox 4.0, IE 9, Opera 10.5 and Safari 5
π Source: github.com/ganqqwerty
Answer:
You can create a function within an outer function (a closure) that allows you to update a private variable but the variable wouldn't be accessible from outside the function without the use of a helper function.
function counter() {
var _counter = 0;
// return an object with several functions that allow you
// to modify the private _counter variable
return {
add: function(increment) { _counter += increment; },
retrieve: function() { return 'The counter is currently at: ' + _counter; }
}
}
// error if we try to access the private variable like below
// _counter;
// usage of our counter function
var c = counter();
c.add(5);
c.add(9);
// now we can access the private variable in the following way
c.retrieve(); // => The counter is currently at: 14
π Source: coderbyte.com
Questions Details:
var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
Answer:
You can create a closure to keep the value passed to the function createBase
even after the inner function is returned. The inner function that is being returned is created within an outer function, making it a closure, and it has access to the variables within the outer function, in this case the variable baseNumber
.
function createBase(baseNumber) {
return function(N) {
// we are referencing baseNumber here even though it was declared
// outside of this function. Closures allow us to do this in JavaScript
return baseNumber + N;
}
}
var addSix = createBase(6);
addSix(10);
addSix(21);
π Source: coderbyte.com
Answer:
A very simply way to check if a number is a decimal or integer is to see if there is a remainder left when you divide by 1.
function isInt(num) {
return num % 1 === 0;
}
console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false
π Source: coderbyte.com
Answer:
The use strict
literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake. For example, the following program will throw an error:
function doSomething(val) {
"use strict";
x = val + 10;
}`
It will throw an error because x
was not defined and it is being set to some value in the global scope, which isn't allowed with use strict
The small change below fixes the error being thrown:
function doSomething(val) {
"use strict";
var x = val + 10;
}
π Source: coderbyte.com
Answer:
A callback
function is a function that is passed to another function as an argument and is executed after some operation has been completed. Below is an example of a simple callback function that logs to the console after some operations have been completed.
function modifyArray(arr, callback) {
// do something to arr here
arr.push(100);
// then execute the callback function that was passed
callback();
}
var arr = [1, 2, 3, 4, 5];
modifyArray(arr, function() {
console.log("array has been modified", arr);
});
π Source: coderbyte.com
Q25: What language constructions do you use for iterating over object properties and array items? ββ
Answer:
For objects:
for
loops -for (var property in obj) { console.log(property); }
. However, this will also iterate through its inherited properties, and you will add anobj.hasOwnProperty(property)
check before using it.Object.keys()
-Object.keys(obj).forEach(function (property) { ... })
.Object.keys()
is a static method that will lists all enumerable properties of the object that you pass it.Object.getOwnPropertyNames()
-Object.getOwnPropertyNames(obj).forEach(function (property) { ... })
.Object.getOwnPropertyNames()
is a static method that will lists all enumerable and non-enumerable properties of the object that you pass it.
For arrays:
for
loops -for (var i = 0; i < arr.length; i++)
. The common pitfall here is thatvar
is in the function scope and not the block scope and most of the time you would want block scoped iterator variable. ES2015 introduceslet
which has block scope and it is recommended to use that instead. So this becomes:for (let i = 0; i < arr.length; i++)
.forEach
-arr.forEach(function (el, index) { ... })
. This construct can be more convenient at times because you do not have to use theindex
if all you need is the array elements. There are also theevery
andsome
methods which will allow you to terminate the iteration early.
Most of the time, I would prefer the .forEach
method, but it really depends on what you are trying to do. for
loops allow more flexibility, such as prematurely terminate the loop using break
or incrementing the iterator more than once per loop.
π Source: github.com/yangshun
Q26: What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript? ββ
Answer:
Some examples of languages that compile to JavaScript include CoffeeScript, Elm, ClojureScript, PureScript, and TypeScript.
Advantages:
- Fixes some of the longstanding problems in JavaScript and discourages JavaScript anti-patterns.
- Enables you to write shorter code, by providing some syntactic sugar on top of JavaScript, which I think ES5 lacks, but ES2015 is awesome.
- Static types are awesome (in the case of TypeScript) for large projects that need to be maintained over time.
Disadvantages:
- Require a build/compile process as browsers only run JavaScript and your code will need to be compiled into JavaScript before being served to browsers.
- Debugging can be a pain if your source maps do not map nicely to your pre-compiled source.
- Most developers are not familiar with these languages and will need to learn it. There's a ramp up cost involved for your team if you use it for your projects.
- Smaller community (depends on the language), which means resources, tutorials, libraries, and tooling would be harder to find.
- IDE/editor support might be lacking.
- These languages will always be behind the latest JavaScript standard.
- Developers should be cognizant of what their code is being compiled toβββbecause that is what would actually be running, and that is what matters in the end.
Practically, ES2015 has vastly improved JavaScript and made it much nicer to write. I don't really see the need for CoffeeScript these days.
π Source: github.com/yangshun
Q27: Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it? ββ
Answer:
Every script has access to the global scope, and if everyone uses the global namespace to define their variables, collisions will likely occur. Use the module pattern (IIFEs) to encapsulate your variables within a local namespace.
π Source: github.com/yangshun
Q28: Why would you use something like the load
event? Does this event have disadvantages? Do you know any alternatives, and why would you use those? ββ
Answer:
The load
event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
The DOM event DOMContentLoaded
will fire after the DOM for the page has been constructed, but do not wait for other resources to finish loading. This is preferred in certain cases when you do not need the full page to be loaded before initializing.
π Source: github.com/yangshun
Questions Details:
Create a for loop that iterates up to 100
while outputting "fizz" at multiples of 3
, "buzz" at multiples of 5
and "fizzbuzz" at multiples of 3
and 5
.
Answer:
Check out this version of FizzBuzz:
for (let i = 1; i <= 100; i++) {
let f = i % 3 == 0,
b = i % 5 == 0;
console.log(f ? (b ? 'FizzBuzz' : 'Fizz') : b ? 'Buzz' : i);
}
π Source: github.com/yangshun
Questions Details:
duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]
Answer:
function duplicate(arr) {
return arr.concat(arr);
}
duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]
π Source: github.com/yangshun
Answer:
The same-origin policy prevents JavaScript from making requests across domain boundaries. An origin is defined as a combination of URI scheme, hostname, and port number. This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page's Document Object Model.
π Source: github.com/yangshun
Answer:
==
is the abstract equality operator while ===
is the strict equality operator. The ==
operator will compare for equality after doing any necessary type conversions. The ===
operator will not do type conversion, so if two values are not the same type ===
will simply return false
. When using ==
, funky things can happen, such as:
1 == '1'; // true
1 == [1]; // true
1 == true; // true
0 == ''; // true
0 == '0'; // true
0 == false; // true
My advice is never to use the ==
operator, except for convenience when comparing against null
or undefined
, where a == null
will return true
if a
is null
or undefined
.
var a = null;
console.log(a == null); // true
console.log(a == undefined); // true
π Source: github.com/yangshun
Answer:
- Native objects are objects that are part of the JavaScript language defined by the ECMAScript specification, such as
String
,Math
,RegExp
,Object
,Function
, etc. - Host objects are provided by the runtime environment (browser or Node), such as
window
,XMLHTTPRequest
, etc.
π Source: github.com/yangshun
Answer:
you can place
"use strict";
at the top of your file in node >= 0.10.7, but if you want your whole app to run in strict (including external modules) you can do this
node --use_strict
π Source: stackoverflow.com
Questions Details:
var err1 = Error('message');
var err2 = new Error('message');
Which one is correct and why?
Answer:
Both are fine; the function call Error(β¦)
is equivalent to the object creation expression new Error(β¦)
with the same arguments.
π Source: stackoverflow.com
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
Q42: Given an array of integers, find the largest product yielded from three of the integers βββ
See π Answer
Q43: Given an array of integers, find the largest difference between two elements such that the element of lesser value must come before the greater element βββ
See π Answer
Q44: Find the intersection of two arrays. An intersection would be the common elements that exists within both arrays. In this case, these elements should be unique! βββ
See π Answer
Questions Details:
For example: Mary
is an anagram of Army
See π Answer
See π Answer
See π Answer
Questions Details:
var y = 1;
if (function f() {}) {
y += typeof f;
}
console.log(y);
See π Answer
See π Answer
Questions Details:
var x = 1;
var output = (function() {
delete x;
return x;
})();
console.log(output);
See π Answer
Questions Details:
var x = { foo : 1};
var output = (function() {
delete x.foo;
return x.foo;
})();
console.log(output);
See π Answer
See π Answer
Questions Details:
(function() {
var a = b = 5;
})();
console.log(b);
See π Answer
Questions Details:
multiply(5)(6);
See π Answer
Q55: Check if a given string is a palindrome. Case sensitivity should be taken into account. βββ
See π Answer
Q56: What's the difference between using βletβ and βvarβ to declare a variable in ES6? βββ
See π Answer
See π Answer
See π Answer
Q59: What are the benefits of using spread syntax in ES6 and how is it different from rest syntax? βββ
See π Answer
See π Answer
See π Answer
See π Answer
Q63: Explain the differences on the usage of foo
between function foo() {}
and var foo = function() {}
βββ
See π Answer
See π Answer
Q65: What is the difference between document load
event and document DOMContentLoaded
event? βββ
Questions Details:
The DOMContentLoaded
event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
window
's load
event is only fired after the DOM and all dependent resources and assets have loaded.
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
Q84: Create a function that will evaluate if a given expression has balanced parentheses using stacks ββββ
See π Answer
See π Answer
Q86: Given an integer, determine if it is a power of 2. If so, return that number, else return -1 ββββ
See π Answer
See π Answer
See π Answer
Questions Details:
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
See π Answer
Questions Details:
var Employee = {
company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);
See π Answer
See π Answer
See π Answer
See π Answer
Q94: How would you add your own method to the Array object so the following code would work? ββββ
Questions Details:
var arr = [1, 2, 3, 4, 5];
var avg = arr.average();
console.log(avg);
See π Answer
Questions Details:
0.1 + 0.2 === 0.3
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
Q103: Explain difference between: function Person(){}
, var person = Person()
, and var person = new Person()
? ββββ
See π Answer
Q104: Can you describe the main difference between a .forEach
loop and a .map()
loop and why you would pick one versus the other? ββββ
See π Answer
Q105: What's the difference between a variable that is: null
, undefined
or undeclared? How would you go about checking for any of these states? ββββ
See π Answer
Q106: Explain why the following doesn't work as an IIFE. What needs to be changed to properly make it an IIFE? ββββ
Questions Details:
function foo(){ }();
See π Answer
See π Answer
See π Answer
See π Answer
Q110: Can you give an example of a curry function and why this syntax offers an advantage? βββββ
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer
See π Answer