JavaScript Interview Prep Kit ?
JavaScript Interview Prep Kit ?
CONCEPTS
Mastering JavaScript
Interview Insights and
Essentials
INTERVIEW HANDBOOK
@UDBHAV SRIVASTAVA
WHAT IS JAVASCRIPT ?
JavaScript, a synchronous single-threaded language,
is a cornerstone in web development, empowering
the creation of interactive and dynamic websites. Its
versatility and efficiency make it a vital tool for
crafting engaging online experiences.
swipe >>
HOW EXECUTION CONTEXT WORKS ?
Here code
var n : undefind n = 10 executed one
line at a time
fun xyz(){
Memory Creation Phase Code Execution Phase
}
Memory Creation Phase Code Execution Phase
fun abc(){
Key : Value
} fun abc(){
here we copy the whole code
Hoisting
Hositing is a phenomenon of Javascript by which we can access
var & Functions even before it’s initialization/put some value in it.
How it Work’s
When variables and functions are declared, they are effectively
"hoisted" to the top of their respective scopes within the memory,
making them accessible throughout the entire scope even before their
explicit placement in the code.
So when there are in Phase 1 there are hoisted.
swipe >>
Why we place javascript files at the end of
the <body> tag ?
It’s a common practice, and it's often recommended
for a couple of reasons:
When a browser encounters a <script> tag, it stops
parsing HTML and executes the script.
Placing scripts at the end of the <body> allows the
HTML content to load and render first
Browsers typically download external resources,
including JavaScript files, in parallel. If scripts are
placed at the end of the <body>, the browser can
start fetching them after the HTML content is already
being displayed.
Async Attribute: Adding the async attribute to a
<script> tag allows the browser to download the
script asynchronously while not blocking the HTML
parsing
swipe >>
UNDEFINED
Undefined is distinct from being empty; rather, it is a special keyword that
holds its own memory space. Consider it as a temporary placeholder,
reserved until the variable is assigned a different value. Until that
assignment occurs, the variable remains in this "placeholder" state known as
undefined.
Lexical Enviroment
The lexical environment is created during the memory creation phase
1 of the execution context. when ever an execution context is created
a lexical enviroment is also created. So, the lexical enviroment is the
local memory along with the lexical enviroment with his parent.
swipe >>
CLOSURES
A closure in JavaScript is formed when a function, along with its
lexical scope, is bundled together. This unique combination
provides the inner function with access to the variables and
parameters of its outer function, even after the outer function
has finished executing
USE’S OF CLOSURE
Closures are powerful mechanisms for
swipe >>
EVENT LOOP
In JavaScript, numerous operations, like timers and network calls,
operate asynchronously. The Event Loop plays a crucial role in
managing these asynchronous tasks efficiently. It ensures that such
operations are handled without blocking the execution of other code,
allowing JavaScript to exhibit asynchronous behavior seamlessly.
swipe >>
BLOCK & SCOPE
A block is used to combine multiple Javascript statement into one, So
that which can use multiple statment in a place where javascirpt expects
one statement.
A block defines a scope, which is the region of code where variables and
functions are accessible.
Hoisted to the top of their scope. Not hoisted in the same way as declarations.
Defined with the function keyword Creates an anonymous function
and a name assigned to a variable.
The primary difference lies in hoisting behavior and whether the function has
a name. Function declarations are hoisted along with their names, while
function expressions (whether anonymous or named) are not hoisted in the
same way.
swipe >>
function (Param , Param){
DIFFERENCE BETWEEN
PARAMETR OR ARGUMENT };
function (Args,Args);
The value which we will be pass inside a function are known as Argument.
These lable and identifier known as Parameter which get those value in the
function.
swipe >>
( ... )
( ... ) this can be Rest parameter or Spread operator.
Rest parameter
if it is in function Arguments then it is Rest parameter it will
combing the statement else everywhere in js it is spread
operator.
Spread operator
Spread operator it will create another copy of that object.
And it will go to new memory
Obj1
Obj2
Deep Clone
In Deep Clone obj2 should not have any refernce of obj1.
It‘s a completely independent copy of an object .
Ob1
const obj2 = JSON.parse(
JSON.stringify(obj1)
);
Ob1
swipe >>
Polyfill of Deep Clone
const deepClone = (obj) => {
Checking the type
const type = typeof obj; of parameter
If parameter is not
if (type !== 'object' || !obj) return obj;
object return it
Function Barrowing :
In function Barrowing we Barrow a function from some other
object and use it with the data of some other object.
swipe >>
THIS KEYWORD
In JavaScript, the this keyword refers to the current execution
context, and its value is determined by how a function is called.
The behavior of this can vary depending on whether the function
is called in the global scope, as a method of an object, with the
new keyword, or using other methods.
Call
Use any fuction which is present in another object.which
explicitly binds functions to any given object
Bind ()
This method returns a new function, where the value of “this”
keyword will be bound to the owner object, which is provided as a
parameter.
Bind method it actually gives us the copy of the method and it does
not invoke it directly
swipe >>
Explain Map (), Filter () & Reduce ()
Map Method ()
A method which runs the given
fucntion over each and every value of
given array and create new array out
of it.
Filter Method ()
Fliter method creates a new array but
selecting elements from an exisiting
array that meets given condition.
It essentially acts as a filter,
Reduce Method ()
It’s technically used at place where we
need to iterate over all the elements of
an Array and find out particular value
swipe >>
Polyfills of Map , Filter & Reduce
Map method expects a call back
Polyfill of Map function from user
Polyfill of Filter
swipe >>
Polyfill of Reduce
callback is the function that will be called for each element in the array, as well as
the accumulator.
InitialValue is an optional parameter. If provided, it is used as the initial value of the
accumulator. If not provided, the first element of the array is used as the initial
value.
If the accumulator is not undefined, it calls the callback function with the current
accumulator, the current element (this[i]), and the index (i). The result becomes the
new value of the accumulator.
swipe >>
Objects
Any value that's not of a primitive type (a string, a number,
a boolean, a symbol, null, or undefined) is an object.
Objects are always passed by reference.
Object Cloning
Here we are creating a copy of an object.
There are three main types of object cloning:
Assigning
It does not create a new
object; instead, both variables
point to the same object in
memory.
Shallow Clone:
swipe >>
Deep Clone:
JSON. stringify
The JSON.stringify function converts a JavaScript object into a JSON
string. It takes an object as input and returns a string representing that
object in JSON format.
JSON.parse
The JSON.parse function does the opposite; it takes a JSON string as
input and converts it into a JavaScript object.
swipe >>
Promise
Promise is an Object representing
the eventual completion or
failure of an asynchronous
operation.
swipe >>
Async & Await
async and await are features in JavaScript that make working with
asynchronous code more readable and easier to reason about.
async Function
The async keyword is used to declare a function as asynchronous.
An async function always returns a promise, and the value of the promise is
the value returned by the function when it completes.
await Operator
The await keyword is used inside an async function to wait for the
resolution of a promise.
It can only be used within an async function, and it pauses the execution of
the function until the promise is resolved.
In this example, fetch returns a promise, and await is used to wait for that promise to
resolve. The second await is used to wait for the JSON parsing to complete.
Error Handling:
When using async and await, error handling can be done using try and catch.
So whenever If any of the promises being awaited rejects, the control jumps to the
nearest catch block.
swipe >>
Debouncing
Delays invoking a function until a certain period has passed without additional
invocation.
Particularly useful in scenarios such as search bars. It helps limit the frequency
of API calls by ensuring that the function is only triggered after a specified
time period has elapsed since the last user input
Throttling
Throttling is like a traffic cop for function calls, making sure they don't
happen too quickly basically managing the frequency of function calls,
ensuring it’s executed at a controlled rate, particularly useful for
performance optimization.
throttling will delay the function call for certain time basically
Debouncing vs Throttling
In debouncing we are ignoring In throttling we are ignoring
last event according to given upcoming event according
conditions. to given conditions.
Wait for a moment of silence Do it, but not too often—
before doing something pace yourself!
Preventing unnecessary API Preventing functions from
calls being called too frequently.
Focuses on ensuring a pause Focuses on controlling the
before action. rate of action.
swipe >>
Udbhav Srivastava
Thanks for reading! If you found value, give it a like, share,
or drop a comment. Your suggestions are appreciated!
Let’s Connect
udbhav-srivastava-a9305321b/