DEV Community

loading...

10 JavaScript Interview Questions you need to know:

shamimaakter profile image Shamima Akter ・3 min read

What are the scopes of a variable in JavaScript?

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.
Global Variable – A global variable has a global scope which means it is visible everywhere in your JavaScript code.
Local Variable – A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

What is the Callback function?

A callback is a plain javascript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘ callback ’. In javascript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions,

What is Closure?

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope. It gives you access to an outer function scope from an inner function. In javascript, closures are created every time a function is created. To use a closure, simply define a function inside another and expose it.

What is the difference between var, let, and const keywords?

Var - The javascript variable statement is used to declare a variable and, optionally, we can initialize the value of that variable. Explain: var a = 20; variable declarations are processed before the execution of the code.
Const – The idea of const functions does not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.
Let – It is single that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.

What is the 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.

What is the difference between null & undefined?

Undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

How will you remove duplicates from a Javascript array?

There are several possible ways of eliminating duplicates from a JS array. The three most used ones are described as follows:
Using filter – It is possible to remove duplicates from an array in javascript by applying a filter to the same. To call the filter () method, three arguments are required. These are namely array, current element, and index of the current element function unque_array ( arr){

For example:
Let unique_array = arr.filter(function(elem, index, self) {
Return index == self.index(elem) ; {
Return unique_array }
Console.log ( unique_array (array_with_duplicates));

By using the For Loop – In this method of removing elements from an array, an empty array is used for storing all the repeating . Example:
elements.Array dups_names = [ ‘Kona ‘ , ‘ Rupa’ , ‘Tumpa’ , ‘Jhorna’ ];
function dups_array( dups_names ) {
let unique = {} ;
names.foreEach(function (i) {
If ( !unique [i] ) {
Unique [i] = true; }
})
Return object.keys (unique); }
Dups_array (names);

By using Set – This is the simplest approach of removing duplicate elements from an array in Javascript. A set is an inbuilt object for storing unique values in an array. Here’s how to use it for eliminating repeating elements from an array: function unique arrays (array) {

What does the new keyword do?

The new keyword is used with the constructor function to make objects in javascript.
The new keyword does 4 things.
•Creates an empty object.
•Assigns that empty object to the this value.
•The function will inherit from functionName.prototype.
•Returns the this if no Explicit return statement is used.

What is DOM? What is the use of document objects?

DOM stands for Document Object Model. A document object represents the HTML document. It can be used to access and change the content of HTML.

What is the difference between undefined value and null value?

Undefined value: A value that is not defined and has no keyword is known as an undefined value. For example:
int number; // a number has an undefined value.
Null value: a value that is explicitly specified by the keyword “ null “ is known as a null value. For example:
String srt = null; // str has a null value.

Discussion (0)

Forem Open with the Forem app