Answers to some of the most common Javascript interview questions:

Search for a command to run...

No comments yet. Be the first to comment.
Image Generated using Midjourney I recently published this article, where I am talking about some MidJourney tricks and tips after which I got some messages saying what I Midjourney. I know my bad, I did not talk about Midjourney a lot in that articl...

Most of these tricks work with BlueWillow, Stable Diffusion, and many other models. Image generated using BlueWillow Midjourney is all over the rage right now, people are going crazy over it and trying different prompts to create beautiful artwork bu...

Photo by Towfiqu barbhuiya on Unsplash This is part 3 of the Managing your Finances article series, if you have not read part 1 and part 2, then I would recommend you to read those before starting with this article. You have started the journey and y...

Photo by micheile henderson on Unsplash This is part 2 of the Managing your Finances article series, if you have not read part 1, then you can read it here, after which you can resume this article. If you are sorted with your current financial situat...

Photo by Mathieu Stern on Unsplash Before starting the journey of Financial Freedom or even improving our finances, we should be prepared by assessing our current financial situation to avoid bumps during our journey. Think of this preparation as col...

Photo by Sebastian Herrmann on Unsplash
Programming interviews are difficult to crack so to help you ace with your next interview as a Javascript Developer I will be sharing answers to some of the common Javascript Interview Questions.
Disclaimer: The answers do not contain technical definitions, these are based on my understanding and reading.
Undeclared variable is a variable without any reference.
Undefined variable is a variable which is declared (referenced) but without any value assigned.
NULL variable is a variable with value
NULLwhich refers to ‘NOTHING’.
Array.forEach() loop and Array.map() methods and why you would pick one versus the other?Array.forEach() returns
undefinedwhere as Array.map() returns the result array after the operations and since it returns the result array, Array.map can be selected when you want to chain the operations on the Array.- adding changes by saud.
Array.forEach(callback) applies the passed callback to each element of the array (generally used as an alternative to traditional loops on array) and returns
undefined.
Array.map(callback) applies the passed callback to each element of the array and
returns a new array with the values returned from the callback.
Hoisting in JavaScript means implicit moving of variables and function declarations to the top of their scope before code execution.
E.g.
// Output
Value of a is undefined
Though the declaration of Variable a is after the function call, instead of throwing reference error, code executes just fine.
== and ===?This is one of the most popular question on stackoverflow.
This is a common feature found in many “dynamically” typed language, Javascript being one of them.
\== or equality operator: checks for the values on the both sides are equal.
\=== or identity operator: checks for the values as well as their data-types on the both sides are equal.
10 == '10' // true
10 === '10' // false
false == 0 // true
false === 0 // false
// here is a funny thing
undefined == null // true
undefined === null // false
A unary operator is an operator with 1 operand
A Binary operator is an operator with 2 operands
A ternary operator is an operator with 3 operands, the word ternary indicates No. of operands.
function isOdd(x) {
return (x%2 == 1) ? true : false
}
console.log(isOdd(10)); // false
console.log(isOdd(11)); // true
// Operands: (x%2 == 1), true and false
// Output
false
true
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
foo1() will return object
{bar: “hello” }where foo2() will returnundefinedbecause;is optional in Javascript, hence in function foo2, the controls are returned after the return statement and remaining code is skipped.
The Event handler defined on a parent element will be delegated / passed-on to all the child elements.
In the above example, you click on either 1st li element or 2nd li element both with show the alert with Thanks!!! message since the event is delegated from parent element to child.
Event handlers on an element are executed in a series starting from it’s own event handler moving towards executing all the event handlers defined on it’s ancestors.
In the above example, if you click on Click Me!!! then it will execute its own event-handler which is alert(“You Clicked first list element!!!”); and then it’s parent’s event-handler which is alert(“Thanks!!!”); .
It’s the opposite of event-bubbling. Event capturing refers to: Event handlers on an element are executed in a series starting from it’s ancestor’s event handler moving downwards executing all the event handlers defined and it’s own event-handler will be executed last. Event capturing can be achieved by setting the 3rd parameter in
addEventHandlermethod to true (default false).
In the above example, if you click on Click Me!!! then it will execute it’s parent element’s event-handler which is alert(“Thanks!!!”);and then it’s own event-handler which is alert(“You Clicked first list element!!!”);.
IIFE stands for Immediately Invoked Function Expression (IIFE). IIFE is a javascript function which executes as and when it’s defined.
let iife_result = (function(){
return 'success'
})();
console.log(iife_result);
// Output
success
In the above example, the function is executed at the time of definition and results are quickly passed and stored in iife_result variable and if you try iife_result() then it will give you the following error: TypeError: iife_result is not a function.
100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5Note: These questions are selected from a very popular GitHub repository: https://github.com/h5bp/Front-end-Developer-Interview-Questions
Thank you for reading…
Live long and prosper!!!