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