Javascript Interview Questions Flashcards

1
Q

Difference between let, var and const

A

“var” is a function scoped and can be redeclared and updated. “let” and “const” are block scoped. “let” can be updated but not redeclared, while “const” can neither be updated nor redeclared.

block is the block its in e.g a while loop or for etc while function is a function scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

== and ===

A

Will just compare values e.g. if you have a string compare to a number, it will convert string to number and compare only the value. === is strict, it compares the type too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

null vs undefined

A

In JavaScript, the term ‘undefined’ refers to a variable that has been declared but has not yet been assigned a value.

null is explicitly assigned by a programmer to indicate that a variable has no value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

purpose of this keyword

A

The “this” keyword is used to refer to the object that it belongs to. The value of “this” varies depending on the context in which it is used. For instance, when used inside a method, “this” refers to the object that the method is called on. When used alone, “this” refers to the global object. Similarly, when used inside a function, “this” also refers to the global object. On the other hand, when used inside an event, “this” refers to the element receiving the event. It is important to note that the value of “this” is determined at runtime and depends on the way in which the function or method is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

method vs function in JS

A

function - block of code to perform a specific task. Executed when something invokes (calls) it.

method - function of an object they have to be assigned to the property of an object. Typically used to update an objects properties or perform operations based on current object properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

asynchronous vs synchronous programming in JS difference

A

Synchronous programming, also known as blocking programming, is a programming model where the code is executed sequentially from top-to-bottom, meaning that the next operation is blocked until the current one completes. This means that the program’s execution is halted until the current operation is completed, and only then can the next operation be executed. This approach can be useful for simple programs that require a linear execution flow, but it can become problematic when dealing with more complex programs.

On the other hand, asynchronous programming is a programming model where the engine runs in an event loop. When an operation blocking is needed, the request starts, and the code keeps running without blocking for the result. This means that the program can continue to execute while waiting for the result of the operation. When the response is ready, the interrupt is fired, causing an event handler to be run, where the control flow continues. This approach helps reach a more efficient use of resources and can be particularly useful when dealing with long-running operations or when working with network I/O. However, it also requires a more complex programming model and can be difficult to understand and debug.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array.map() purpose

A

creates a new array by calling a function on each element in the original array, store the returned values in the new array. typically when you want to apply a transformation to all array elements. e.g convert all letters to uppercase.

process does not alter original array, creates a new array. map function is a pure function. no side effects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

arrow vs regular function

A

syntax - shorter syntax, can omit function keyword and return.

THIS keyword - regular functions THIS shows the object called the function, could be document window or whatever. THIS doesn’t bind its value in arrow functions, inherits from enclosing lexical / global context. apparently easier to predict behaviour but I don’t think so.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

explain MEMOIZATION

A

smart way to make functions return faster by storing and reusing their past results. Like using a memory system, if a function is asked the same question again it retrieves the data from memory instead of recalculating it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

data binding

A

data synchronization between model and view. model and view linked to allow automatic data sync between the model and view.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

expression vs statement

A

expression is a set of literals, variables, operators etc that evaluate to a single value. A statement does something e.g. it can make a function call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

strict mode in JS

A

Strict mode is a feature in JavaScript that was introduced in ECMAScript 5. It lets you write code in such a way that follows stricter rules.

When strict mode is enabled, the JavaScript engine enforces additional constraints. This means you may be able to catch some common errors that would have otherwise gone unnoticed. It also helps you write cleaner and more secure code.

// Strict mode

function strictFunc() {
“use strict”
username = “Marie”
console.log(username)
}

strictFunc()

uncaught reference error – doesn’t allow hoisting :O

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you explain how JavaScript handles asynchronous operations

A

callbacks, promises and async wait.

callbacks - passed as args to other functions, invoked after completion of some operation. Ensures something is not executed until a necessary operation is complete.

Promises - an object that represents eventual completion or failure of async operation, and its resulting value. you can chain ops together and handle errors with these.

Async wait - syntactic sugar ontop of a promise. Uses async keyword to declare a function that returns a promise and await to wait for a promise to resolve before continuing with execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain closures

A

A function that has the ability to access its own scope. As well as the scope of the functions that surround it and the global scope.

Allows for functions to remember and access variables from an outer function even after the outer function has been executed.

outer function and variables and parameters are accessible within the inner function, even when that outer function is done running.

can cause things like memory leaks.

function numberGenerator() {
// Local “free” variable that ends up within the closure
var num = 1;

function checkNumber() {
console.log(num);
}

num++;
return checkNumber;
}

var number = numberGenerator();
number(); // 2

checkNumber forms a closure over the num variable because checkNumber is defined within numberGenerator and thus retains access to num, even after numberGenerator has returned.
The value of num when checkNumber is invoked is 2, which is the value num had at the end of the execution of numberGenerator.

Preservation of Lexical Scope: Closures allow functions to access variables from their lexical scope, even after the outer function has finished executing.

State Retention: The variable num retains its value between calls to checkNumber because the closure captures num and its state.

Encapsulation: Closures help in encapsulating state and functionality. In this example, num is kept private and protected from external modification, providing a controlled way to access and manipulate it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does JavaScript’s event loop work

A

mechanism for managing async operations and executing callbacks non blocking. Single threaded loop that continuously monitors the call stack and callback queue.

call stack - data structure that tracks execution of functions in js. Whenever a function invoked, pushed onto the call stack. When completes execution, popped off the stack.

OTHER HAND The callback queue - holds a list of functions ready to be realized once the call stack is empty. When an async op, such as event listener or network request, completes execution, it associated callback function pushed onto callback queue.

When callstack empty, even loop takes first function from the callback queue, pushing it to the call stack which runs it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain HOISTING

A

lets do example

printHello()

function printHello(){ console.log(“hello”)}

here we executed printHello before we declared it, and everything still works. No Errors!

before interpreter executes the whole code, it hoists (lifts up) declared function to the top of the scope it is defined in

Variable hoisting –

var, let and const variables - these are hoisted but behave differently.

e.g var

console.log(name);
// undefined

var name = “Namo”

with var, the variable declaration is hoisted but with a value of undefined, the actual value is initalised when declaration line is executed. So you can do something like this -

console.log(name);

var name = “Bob”

console.log(name)
// Bob

we can access the name after instantiation

lets say name function

print()

console.log(name)
// reference error - name not defined.

function print(){ var name = “bob” }

variables hoisted only to the top of the scope they are declared in. here name is declared in print so only goes to the top of that local scope.

access in function -

print()

function print(){ console.log(name)
// undefined
var name = “bob”}
——————–

hoisting let variables - they can be hoisted but have different behavior

console.log(name)

// cannot access before initialization

let name = “bob”

so the interpreter is aware of it as it is hoisted but let values have no default value when hoisted. variables hoisted to the top of the scope they are declared in (local, global, block) but not accessible because they have not been initialised. – TEMPORAL DEADZONE> only access after executed. Const behaves the same. Same for classes.

17
Q

Promises

A

An object representing an async operation eventual completion or failure. Like a placeholder for the outcome of operations.

Every promise has one of three states - pending, fulfilled and rejected.

Pending - async op not yet completed and final result is unknown. Once op completes successfully, the promise is fulfilled and final result is available. If op fails or error, goes to rejected state.

18
Q

Spread operator

A

spread elements of array or iterable into places where 0 or more are expected e.g. spread properties of an object into a new object

18
Q

Lexical Scope

A

Scope determined by variables position in source code. determined at compile time, not runtime. …

19
Q

immutability

A

cant be changed once a value is created. Helps avoid side effects and make program more predictable.

20
Q

explain destructuring

A

unpack values from arrays or properties from objects into distinct variables.

e.g. arr = [1,2,3] you could do let a = arr[0] b= a[1] etc but instead you can do let [a, b, c] = arr so you split out the items into separate variables easily.

same with objects

let name = obj.name etc but you can do let {name, age} = obj

21
Q

static vs instance method

A

static method is bound to its class, not an instance of that class. E.g. you don’t need to instantiate class to use it. Instance method is available on the instance of the class, not the class itself