Javascript Deck 3 Flashcards
What is a higher order function?
What is a void function?
What does the return statement do?
What is a class?
What is the difference between null and undefined?
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
What is hoisting in JavaScript?
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;
What is closure in JavaScript?
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
What is the difference between synchronous and asynchronous code in JavaScript?
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
What is event bubbling in JavaScript?
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
What is the difference between let, const, and var?
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
What is the difference between == and === operators?
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
What is the difference between a function declaration and a function expression in JavaScript?
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
What will be the output of the following code?
console.log(2 + true);
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.
What will be the output of the following code?
console.log([] == ![]);
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.
What will be the output of the following code?
console.log(NaN === NaN);
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.