DEV Community

Cover image for Important JavaScript Interview Q&A
Himanshu Tiwari ๐ŸŒผ
Himanshu Tiwari ๐ŸŒผ

Posted on

Important JavaScript Interview Q&A

Ahaan, No time waste let's directly check those questions.
Alt Text

Q1- What is a callback?


Callback functions are passed as arguments of other functions and they are normally executed at the end of the parent function.

For an example check this

Q2- What is CORS?


Cross-Origin Resource Sharing or CORS is a mechanism that uses additional HTTP headers to grant a browser permission to access resources from a server at an origin different from the website
origin.

An example of a cross-origin request is a web application served from http://myowndomain.com that uses AJAX to make a request for http://yourbaddomain.com.

Q3- Evaluate 0.1 + 0.2 === 0.3 ?


It evaluates to false because JavaScript uses the IEEE 754 standard for Math and it makes use of 64-bit floating numbers. This causes precision errors when doing decimal calculations, in short, due to computers working in Base 2 while decimal is Base 10.

0.1 + 0.2  
//OUTPUT
//0.30000000000000004
Enter fullscreen mode Exit fullscreen mode

Q4- What is the difference between lexical scoping and dynamic scoping ?


Lexical scoping or Static scoping refers to when the location of a function's definition determines which variables you have access to. On the other hand, dynamic scoping uses the location of the function's invocation to determine which variables are available.
For better source code we often use Lexical scoping.

Q5- What is the difference between undefined and null ?


The major difference - null is explicit while undefined is implicit. Let's say we have a variable and we don't have a value associated with that, so that makes it undefined but null is set as the value to explicitly indicate โ€œno valueโ€. undefined is used when the nothing is "not" known, and null is used when the nothing is known.

Q6- Does JavaScript pass by value or by reference?


Pass by value always.However, with objects, the value is a reference to the object.

Q7- What is the output of the following code?

const a = [0, 1, 2]
const b = [0, 1, 2]
const c = "0,1,2"

console.log(a == c)
console.log(a == b)
Enter fullscreen mode Exit fullscreen mode

TRUE - automatic type conversion
FALSE - Arrays are compared by reference

Q8- Are semicolons required in JavaScript?


Sometimes
Semicolons are usually optional in JavaScript but have edge cases where they are required.

If you don't use semicolons, tools like Prettier will insert semicolons for you in the places where they are required to save in a text editor to prevent errors.

Q9- What does the following code evaluate to?

typeof typeof 0
Enter fullscreen mode Exit fullscreen mode

It evaluates to "string".
typeof 0 evaluates to number
and typeof "number" evalutes to string

ALT TEXT
Enough for today
But not for JavaScript ;)
If you have some amazing Questions to add, let's add them in the comments which will be beneficial for everyone.

Discussion (3)

Collapse
gouravsingh2580 profile image
Gourav Singh

curation on point

Collapse
himanshutiwari15 profile image
Himanshu Tiwari ๐ŸŒผ Author

Is that a praise or a sarcasm ๐Ÿง

Collapse
gouravsingh2580 profile image
Gourav Singh

praise