DEV Community

Aneeqa Khan
Aneeqa Khan

Posted on

Mostly asked JavaScript conceptual questions in interviews

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

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

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

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

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

What is Scope

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 link

What is closure

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

What is callback

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

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. 😊

Top comments (3)

Collapse
 
andyc profile image
Andy Cormack • Edited

Hi, I have to disagree with you about pass by reference, objects and arrays are indeed passed by reference in function parameters. Simply because you can reassign what the local variable inside the function points to does not invalidate the fact that you can mutate the passed external object or array from inside that function.

This is a potentially dangerous "gotcha", and while you do state that you can mutate the object's properties, you need to acknowledge that they are passed by reference.

Don't let this detract from your article overall though, I enjoyed the read and these are definitely useful ones to learn fully, not just for the interview stage, but for understanding the fundamentals of how javascript works. So thank you for the time and effort you put in!

Collapse
 
huydzzz profile image
Pơ Híp

great article

Collapse
 
devved2017 profile image
ved

your explanation is so confusing, needs clarity, every reader will get more confused after reading this article