Mostly asked JavaScript conceptual questions in interviews
Tags: javascript, beginners
Published: 2021-05-26 20:09:53

I recently gave some interviews and mostly all interviewer asked these similar questions regarding JavaScript. So I want to share my knowledge with you all through this blog.

* [JS is single threaded or multi threaded](#JS is single threaded or multi threaded)
* [JS is synchronous or asynchronous](#JS is synchronous or asynchronous)
* [JS is passed by value or passed by reference](#JS is pass by value or pass by reference)
* [What is Hoisting](#What is Hoisting)
* [What is Scope](#What is Scope)
* [What is closure](#What is closure)
* [What is callback](#What is callback)
* [Async/Await](#Async/Await)

## JS is single threaded or multi threaded <a name="JS is single threaded or multi threaded"></a>
Javascript is single-threaded which means it has only one call stack. The call stack is the same as the stack data structure and stacks are FILO that is First In Last Out. Similarly, within the call stack, whenever a line of code gets inside the call stack it gets executed and move out of the stack. In this way, JavaScript is a single-thread language because of only one call stack.

## JS is synchronous or asynchronous <a name="JS is synchronous or asynchronous"></a>
Since JavaScript is a single-threaded language, it is synchronous in nature. As the name suggests synchronous means to be in a sequence, i.e. every statement of the code gets executed one by one.
JavaScript is only asynchronous in the sense, for example, image processing or making requests over the network like API calls.

##  JS is passed by value or passed by reference <a name="JS is pass by value or pass by reference"></a>
In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the passing variables into arguments inside the function. Any changes that you make to the arguments inside the function do not affect the passing variables outside of the function.
But when you pass an object and change its members, those changes persist outside the function. This makes it look like passed by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it's really passed by value.

##  What is Hoisting <a name="What is Hoisting"></a>
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.
But the hoisting mechanism only moves the declaration. The assignments are left in place. To read more about it, click [here](https://www.digitalocean.com/community/tutorials/understanding-hoisting-in-javascript)

##  What is Scope <a name="What is Scope"></a>
The scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.
There are two types of scopes.
Global Scope: A variable is in the Global scope if it's defined outside of a function.
Local Scope: Variables defined inside a function are in the local scope.
To read more about scope, refer to [this](https://scotch.io/tutorials/understanding-scope-in-javascript#toc-what-is-scope-) link 

##  What is closure <a name="What is closure"></a>
A closure is the combination of a function and the lexical environment within which that function was declared. In other words, Closure is an inner function that has access to the outer (enclosing) function's variables.
Closures are used to extends the behavior of outer functions and are useful when working with events.
To read about closures in-depth, refer to [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures)

##  What is callback <a name="What is callback"></a>
A callback is a function that executes after another function has executed. Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed. This task can be any API calls or any task based on timers.

##  Async/Await <a name="Async/Await"></a>
Async/await is basically the syntactic sugar on top of Promises. ES5 handled asynchronous patterns with callbacks, ES6 handled it with promises and now ES7 provides us async/await to work with asynchronous tasks.
Each async function returns a promise and every single item you get from await is a promise as well. And async/await also reduces callback hell caused by callback functions.
So here are few concepts I learned from different sites and mentioned few others too above. Feel free to share your thoughts below. :blush: