Javascript Flashcards

Cards from ChatGPT

1
Q

Can you explain the difference between let, const, and var in JavaScript? When would you use each one?

A

‘let’ is used for variables that can be reassigned, ‘const’ is used for variables whose value cannot be changed once assigned, and var is similar to ‘let’ but has some differences in terms of scope.

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

How you would check the data type of a variable in JavaScript?

A

Checking the data type of a variable can be done using the typeof operator.

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

What is the difference between == and === in JavaScript?

A

In JavaScript, == is the equality operator, which performs type coercion if the operands are of different types, while === is the strict equality operator, which checks both the value and the type without coercion.

console.log(5 == “5”); // true (coercion)
console.log(5 === “5”); // false (strict comparison)

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

Write a short code snippet using the ternary operator (? :) to check if a number is even or odd.

A

The ternary operator (? :) is a concise way to write an if…else statement. Here’s an example to check if a number is even or odd:

let number = 10;
let result = (number % 2 === 0) ? “even” : “odd”;
console.log(result); // This will output “even”

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

What is the difference between function declarations and function expressions?

A

Function declarations are defined using the function keyword followed by a function name and parameters. Function expressions, on the other hand, involve assigning a function to a variable, often using anonymous functions.

const multiply = function(num1, num2) {
return num1 * num2;
};
console.log(multiply(3, 5)); // This will output 15

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

How do you add an element to the end of an array in JavaScript?

A

You can add an element to the end of an array using the push() method. For example:

let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // This will output [1, 2, 3, 4]

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

Write a code snippet to loop through an array and log each element to the console.

A

To loop through an array and log each element to the console, you can use a for loop or array iteration methods like forEach(). Here’s an example with forEach():

let arr = [1, 2, 3];
arr.forEach(function(element) {
console.log(element);
});
// This will log:
// 1
// 2
// 3

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

What are the benefits of calling functions anywhere in the code?

A

Readability: By allowing function declarations to be hoisted, JavaScript makes it easier for developers to understand the flow of the code. When you see a function call, you don’t have to search through the entire codebase to find where the function is defined. This can make the code more readable and easier to maintain.

Organization: Function declarations at the top of a script or function scope can serve as a sort of “table of contents” for the code that follows. Developers can quickly see what functions are available and how they are used, making it easier to navigate and reason about the code.

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

Which statement is true about for loops?

A

For loops are appropriate when looping a predetermined number of times

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

What is the general purpose of a loop?

A

Loops automatically iterate a block of code based on conditions

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

What do nested for loops do?

A

Nested for loops allows us to run multiple for loops at once

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

Which statement is true about while loops?

A

while loops evaluate a condition for however long it’s true and the looping stops when the condition is false

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

You want to run a code block at least once, then loop as long as a condition remains true. Which kind of loop would you use?

A

A do…while statement

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

Object.entries()

A

Returns an array containing nested arrays of all the key-value pairs of an object.

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

Object.assign()

A

Can be used to copy all of the properties of an object into a new object

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