Important JavaScript Interview Q&A 
Tags: javascript, interview, webdev, devops
Published: 2021-02-01 01:00:01

Ahaan, No time waste let's directly check those questions.
![Alt Text](https://thumbs.gfycat.com/ImmediateYellowishGar-small.gif)
##<center>Q1- What is a callback?</center>
<hr style="border:2px solid gray"> </hr>
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](https://www.freecodecamp.org/news/what-is-a-callback-function-in-javascript/)

##<center>Q2- What is CORS?</center>
<hr style="border:2px solid gray"> </hr>
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`.

##<center>Q3- Evaluate 0.1 + 0.2 === 0.3 ?</center>
<hr style="border:2px solid gray"> </hr>
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.
```javascript
0.1 + 0.2  
//OUTPUT
//0.30000000000000004
```

##<center>Q4- What is the difference between lexical scoping and dynamic scoping ?</center>
<hr style="border:2px solid gray"> </hr>
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.

##<center>Q5- What is the difference between `undefined` and `null` ?</center>
<hr style="border:2px solid gray"> </hr>
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.

##<center>Q6- Does JavaScript pass by value or by reference?</center>
<hr style="border:2px solid gray"> </hr>
`Pass by value` always.However, with objects, the value is a reference to the object.

##<center>Q7- What is the output of the following code?
```javascript
const a = [0, 1, 2]
const b = [0, 1, 2]
const c = "0,1,2"

console.log(a == c)
console.log(a == b)
```</center>
<hr style="border:2px solid gray"> </hr>
TRUE - automatic type conversion
FALSE - Arrays are compared by reference

##<center>Q8- Are semicolons required in JavaScript?</center>
<hr style="border:2px solid gray"> </hr>
_Sometimes_
Semicolons are usually optional in JavaScript but have edge cases where they are required.

If you don't use semicolons, tools like [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) will insert semicolons for you in the places where they are required to save in a text editor to prevent errors.

##<center>Q9- What does the following code evaluate to?
```javascript
typeof typeof 0
```</center>
<hr style="border:2px solid gray"> </hr>
It evaluates to "string".
`typeof 0` evaluates to number
and `typeof "number"` evalutes to string

![ALT TEXT](https://i.gifer.com/Ss1b.gif)
`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.