Top Java Script Interview Questions - Solved

With the advancements in devices and different screen sizes, frontend development has become more and more popular. JavaScrit is the most popular language for frontend development with demands getting increased. This page will cover interview questions and tips for professionals looking for job in the field of frontend development. We encourage you to grasp these concepts and logics and do a hands-on before going for an interview.

Q: What are closure in javascript? Explain with example.

Closure is a fuctionality or technique in javascript with the help of which a function is able to remember the value or keep the value of the varible last calculated declared within the parent function even when the parent function execution is completed.

let's consider the below example:



In the above example, we have a parent function called "createConter()" and a child function called "increment(). Variable "count" is declared and initialized within parent function and incremented in child function.

We are then calling the parent function and assigning it to the const variable "counter"

Now with the concept of closure, when we output the counter like console.log(counter())
It will give is "1" for the first time. When we again run the same, it will incremt the count and give us "2". this is because the child function remembers the last incremented value.

If we declar one more variable like : const counter2 = createCounter(); , and call console.log(counter2()) then it will give us "1" as called for the first time for the new constant.

Q:What is the difference between synchronous and asynchronous code in JavaScript?

When there are multiple tasks to be performed and tasks are performed in one by one fashion i.e. in sequential order. Each process is dependent on the completion of the previous process which means tasks or processes are in sync with each other and this type of code is termed as synchronous code.

Whereas, different tasks are independent of each other in asynchronous code, allowing the tasks to run in parallel. When the asynchronous function is called, the program will continue without waiting for the function to return a result or output. Instead, it will continue to the next line of code and the async function will be executed in the background.

Below is an example of synchronous code in JavaScript:


const result = syncFunction();
console.log(result);
### The program is dependent on syncFunction to return a result


In the above example, the syncFunction is a synchronous function that is being called and output is stored in the given variable. The program will stop and wait for the function to return a result before logging it to the console.

Below is an example of asynchronous code in JavaScript:

asyncFunction((err, result) => { if (err) {
console.error(err);
} else {
console.log(result); ### The program will not wait for asyncFunction to give an output
}
});

In the above example, the asyncFunction is an asynchronous function which is being called and its output is passed to a callback function. The program will not stop and wait for the function to return a result, but will continue to the next line of code and execute the callback function when the async operation is complete.

Synchronous code is easier to understand and debug, but it can hold the program from doing other tasks until the synchronous operation is complete which will also be time consuming. Asynchronous code allows the program to do other tasks while waiting for the async operation to complete, but it can be more complex to understand and debug.

Q: Explain the difference between a var, let, and const in JavaScript.

In JavaScript, var, let and const are used to declare variables but they have differences in terms of their scope and mutability:

var: 'var' is a depreciated keyword which was used to declare variable in pre ES6 code i.e before modern javascript as now a days this has been avoided by developers as it is prone to bugs. Scope of the variable declared with 'var' is limited to the function in which it is declared or in other words 'var' keyword can be termed as function-scoped. Any variable declared with 'var' is outside of any function, it will be considered as a global variable and is accessible from anywhere within the code. Moreover, variables declared with var are mutable, which means that their value can be changed after they are initialized. Can also be redeclared and updated within the function

example: var name="John"
var age=23

let: 'let' keyword is used to declare block scope variables in JavaScript. Variables declared with let are block-scoped, which means that they are only accessible within the block in which they are declared. This is in contrast to var, which is function-scoped. Like var, let variables are mutable and their value can be changed after they are initialized.

example: let salary=1000
let job="developer"
you can use let at the time of creation of the variable, but if you want to update the value, you can directly use variable name without using let for the same variable. if you use let again for the same variable, code will throw an error.
salary=10000 directly updating the variable salary.

const: 'const' keyword is used to declare constants in JavaScript which when hardcoded will remain constant throughout the code, like a variable that cannot be re-assigned after they are initialized. Constants declared with const are block-scoped, similar to let.

example: const version=1

Q: How one can create an array in JavaScript.

Array can be created with the help of square brackets and can be assigned to a variable. Array is predefined object in javascript.
example:
var fruits=["a", "b", "c"];
var veggies=[];
Below is the example code of javascript explaining how array is being used and how to get an element in an array and how to update element in a javascript array :

Q: What is the advantage and use of callback function in JavaScript.

Callback function is a function which acts as a parameter to another function. This another function will be higher in order to the callback function.
It will be executed when the main function, where callback function is passed as a parameter, gets executed.
callback function helps the application to understand when to wait or halt and when to continue with the execution. With the help of callback function, an application can work in asynchronous fashion.
Below is the example of callback function being used in javascript. Below code will greet the person forst before saying goodbye :

Q: How would you add your javascript code to you website or html page ?

For generic use of the script, you can create a separate file with your javascript code and use or call that file in your html page under head tag.
One can also write the code inside script tag inside the head or body tag within HTML file
Creating separate file for javascript code is better for code readability as well as management

Q: Differentiate undefined with null in javascript.

Difference between undefined and null is that in undefined, variable is already declared but value has not been assigned to that variable. This type of variable will be termed as undefined variable.
Null is a value which can be assigned to a variable to indicate that variable is defined but it has no value or null value.
Below is the example javascript code for this:

Q: What are arrow function in JavaScript ? Give example also.

An easy way of writing a function in javascript is by arrow function with the help of which one can write function in concise and convenient manner.

Lets coonsider an example of a normal function where we have declaration and return statement:


Now, let build the same function using arrow function :



From above we can see that the arrow function doesn't need declaration and return statement. This is more useful when we have simple operations.

Drawbacks:

* 'This' binding feature is not there which would be required in complex functionalities.
* 'arguments' is not accessible to arrow function
* 'Unlike' traditional functions, one can not use 'new' keyword of javascript to create instance of objects without which it cannot be used as constructors.