Javascript Deck 3 Flashcards

1
Q

What is a higher order function?

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

What is a void function?

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

What does the return statement do?

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

What is a class?

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

What is the difference between null and undefined?

A

Null and undefined are both JavaScript data types that are used to represent the absence of a value. However, they differ in their meaning. Undefined means a variable has been declared, but it has not been assigned a value. Null, on the other hand, is an assignment value that represents no value or an empty value.

Code Example:

let a;
console.log(a); // Output: undefined

let b = null;
console.log(b); // Output: null

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

What is hoisting in JavaScript?

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 you can use a variable or a function before it is declared.

Code Example:

console.log(a); // Output: undefined
var a = 10;

The above code is equivalent to the following code:

var a;
console.log(a); // Output: undefined
a = 10;

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

What is closure in JavaScript?

A

A closure is a function that has access to the outer function’s variables, even after the outer function has returned. This is possible because the inner function has a reference to the outer function’s variables. Code Example:

function outer() {
let a = 10;
function inner() {
console.log(a);
}
return inner;
}

let innerFunc = outer();
innerFunc(); // Output: 10

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

What is the difference between synchronous and asynchronous code in JavaScript?

A

Synchronous code is executed in a sequence, one after the other. Asynchronous code, on the other hand, is executed out of sequence, with some code running in the background while the rest of the code continues to execute.

// Synchronous code:

console.log(‘Start’);
console.log(‘Middle’);
console.log(‘End’);

// Output:
// Start
// Middle
// End

// Asynchronous code:

console.log(‘Start’);
setTimeout(() => {
console.log(‘Middle’);
}, 1000);
console.log(‘End’);

// Output:
// Start
// End
// Middle

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

What is event bubbling in JavaScript?

A

Event bubbling is a phenomenon where an event that is triggered on a child element is also triggered on its parent elements. This is because events “bubble up” from the child element to its parent elements.

Code Example:

HTML:

<div>
<div>
Click me
</div>
</div>

JavaScript:

let parent = document.querySelector(‘#parent’);
let child = document.querySelector(‘#child’);

child.addEventListener(‘click’, () => {
console.log(‘Child clicked’);
});

parent.addEventListener(‘click’, () => {
console.log(‘Parent clicked’);
});

// Output:
// Child clicked
// Parent clicked

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

What is the difference between let, const, and var?

A

Let, const, and var are all used for declaring variables in JavaScript, but they differ in their scoping and hoisting behavior. Var declarations are hoisted to the top of their scope, while let and const declarations are not. Const declarations cannot be reassigned once they are declared, while let and var declarations can be reassigned.

var a = 10;
let b = 20;
const c = 30;

function example() {
console.log(a); // Output: undefined
console.log(b); // Output: ReferenceError: b is not defined
console.log(c); // Output: 30

var a = 1;
let b = 2;
const c = 3;
}

example();

console.log(a); // Output: 10
console.log(b); // Output: ReferenceError: b is not defined
console.log(c); // Output: ReferenceError: c is not defined

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

What is the difference between == and === operators?

A

The == operator checks if the operands are equal, but it performs type coercion if the operands are of different types. On the other hand, the === operator checks if the operands are equal and of the same type.

Code Example:

console.log(1 == ‘1’); // Output: true
console.log(1 === ‘1’); // Output: false

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

What is the difference between a function declaration and a function expression in JavaScript?

A

A function declaration is a function that is declared as a statement, and it is hoisted to the top of its scope. A function expression, on the other hand, is a function that is assigned to a variable, and it is not hoisted.

Code Example:

Function declaration:

function sayHello() {
console.log(‘Hello’);
}

Function expression:

let sayHi = function() {
console.log(‘Hi’);
};

sayHi(); // Output: Hi

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

What will be the output of the following code?

console.log(2 + true);

A

The output will be 3.

When JavaScript encounters a + operator with a number and a boolean, it automatically converts the boolean to its numeric equivalent (true becomes 1). Therefore, the expression becomes 2 + 1, which evaluates to 3.

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

What will be the output of the following code?
console.log([] == ![]);

A

Answer :

The output will be true.

Explanation:

This is an example of a tricky comparison involving truthy and falsy values. The ! operator negates the truthiness of an object. An empty array [] is truthy, so ![] evaluates to false. When comparing [] with false, JavaScript performs type coercion, converting false to a numeric value (0). Thus, the expression becomes [] == 0, and due to another type coercion, the empty array is converted to an empty string ‘’, which is also converted to the numeric value 0. Hence, the expression 0 == 0 evaluates to true.

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

What will be the output of the following code?

console.log(NaN === NaN);

A

Answer :

The output will be false.

Explanation:

NaN (Not a Number) is a special numeric value in JavaScript, and interestingly, NaN is not equal to itself. Therefore, the expression NaN === NaN evaluates to false.

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

What will be the output of the following code?
console.log(‘5’ - - ‘3’);

A

Answer :

The output will be 8.

Explanation:

The unary - operator can be used to convert a string to a number. In this case, - ‘3’ converts the string ‘3’ to the number -3. Then, the expression becomes ‘5’ - (-3), which is equivalent to ‘5’ + 3. JavaScript performs string concatenation since one of the operands is a string, resulting in the string ‘53’. Finally, the result is converted to a number, yielding 8.

17
Q

What will be the output of the following code?

console.log(null == undefined);

A

Answer :The output will be true.

18
Q

what is a reference in javascript?

A
19
Q

what is scope in javascript?

A
20
Q

Fetch vs Axios

A

Fetch: A built-in JavaScript API for making HTTP requests. It returns a promise and requires manual error handling.
Axios: A popular third-party library with more features like automatic JSON transformation, request/response interceptors, and better error handling.

21
Q

Rest Operator & Spread Operator

A

Rest Operator (…): Collects all remaining elements into an array. Example: function(…args) {}.
Spread Operator (…): Spreads elements of an iterable (like an array) into individual elements. Example: […array].

22
Q

Closures

A

A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. It allows the function to access variables from the outer function where it was created.

23
Q

Promises

A

Promises represent the eventual completion (or failure) of an asynchronous operation and its resulting value. They have three states: pending, fulfilled, and rejected.

24
Q

Event Loop

A

The event loop is a mechanism in JavaScript that handles asynchronous code execution. It continuously checks the call stack and task queue, executing code when the call stack is empty.

25
Q

Array Methods

A

Common JavaScript array methods include map(), filter(), reduce(), forEach(), find(), and some(). These methods provide powerful ways to iterate and manipulate arrays.

26
Q

Event Bubbling

A

Event bubbling is the process where an event triggers on the deepest target element and then propagates up through its ancestors in the DOM tree, triggering event listeners along the way.

27
Q

DOM Manipulation

A

DOM manipulation involves using JavaScript to dynamically change the structure, style, or content of a webpage by selecting and altering elements in the Document Object Model.

28
Q

Prototypal Inheritance

A

Prototypal inheritance is a feature in JavaScript where objects inherit properties and methods from other objects. Objects can directly inherit from other objects without the need for classes.

29
Q

Critical Rendering Path

A

The critical rendering path is the sequence of steps the browser takes to convert HTML, CSS, and JavaScript into pixels on the screen. Optimizing this path improves page load times and performance.

30
Q

Functional Programming

A

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. It emphasizes immutability, first-class functions, and the avoidance of side effects.

31
Q

: What is the difference between Pure and Impure functions in JavaScript?

A

Pure Function: A function that, given the same inputs, always returns the same output and has no side effects (does not modify external state or variables).
Impure Function: A function that can return different outputs for the same inputs or has side effects, such as modifying external state or variables.

32
Q

JavaScript Runtime in the Browser

A

**1. ** JavaScript Engine:

The browser uses a JavaScript engine (like Chrome’s V8) to execute JavaScript code. This engine reads the code, converts it into machine code, and then runs it.
**2. ** Single-Threaded Event Loop:

JavaScript operates on a single thread, meaning it can do one thing at a time. To handle multiple tasks, it uses an event loop that manages and executes events, such as user actions, timers, or network requests.
**3. ** Call Stack:

The call stack keeps track of function calls in the order they’re executed. When a function is called, it’s added to the stack; when it finishes, it’s removed.
**4. ** Web APIs:

The browser provides Web APIs (like setTimeout, fetch, or DOM manipulation) that JavaScript can use. These APIs are asynchronous, meaning tasks like network requests can be offloaded and executed without blocking the main thread.
**5. ** Callback Queue:

Once asynchronous tasks are completed, their associated callbacks are placed in the callback queue, waiting to be executed once the call stack is clear.
**6. ** Event Loop Mechanism:

The event loop continuously checks if the call stack is empty. If it is, it moves the first task from the callback queue to the call stack, allowing the JavaScript engine to execute it.
**7. ** Rendering:

The browser’s rendering engine works alongside the JavaScript engine to update the UI. JavaScript can trigger reflows (layout changes) and repaints (visual updates), but these operations are optimized to avoid blocking the main thread.
In summary, the JavaScript runtime in the browser efficiently manages synchronous and asynchronous operations using a single-threaded event loop, call stack, and callback queue, allowing it to handle complex tasks without freezing the UI.