fundaments Flashcards

1
Q

Explain the difference between var, let, and const.

A

var:
Function-scoped (or globally scoped if not within a function).
Can be re-declared and reassigned.
Hoisted to the top of the function/global scope.
let:
Block-scoped.
Can be reassigned but not re-declared within the same scope.
Not hoisted.
const:
Block-scoped.
Cannot be reassigned or re-declared.
Not hoisted.

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

What are primitive data types in JavaScript?

A

Number
String
Boolean
Null
Undefined
Symbol (ES6)
BigInt (ES10)

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

What is the difference between == and ===

A

==: Performs type coercion (attempts to convert operands to the same type before comparison).
===: Strict equality comparison.
(Returns true only if both operands are of the same type and have the same value)

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

Explain how hoisting works in JavaScript.

A

Hoisting is a JavaScript behavior where variable declarations are moved to the top of their scope (function or global) during the compilation phase.
var declarations are hoisted, but their initial value remains undefined.
Function declarations are also hoisted.

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

Describe how if, else if, and else statements work

A

if: Executes a block of code if a specified condition is true.
else if: Executes a block of code if the previous if condition is false and the current else if condition is true.
else: Executes a block of code if none of the preceding if or else if conditions are true.

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

Explain how for, while, and do…while loops work.

A

for: Executes a block of code a specified number of times.
while: Executes a block of code as long as a specified condition is true.
do…while: Executes a block of code
once, then repeats the execution as long as a specified condition is true.

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

What is a ternary operator?

A

The ternary operator ( ? : ) is a concise way to write a conditional expression.
condition ? expressionIfTrue : expressionIfFalse

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

Explain how to define a function in JavaScript.

A

function functionName(parameter1, parameter2) { // code to be executed return value; }

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

What are arrow functions and how do they differ from regular functions?

A

Arrow functions provide a more concise syntax for writing functions.
They have a different this binding compared to regular functions.

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

What is the purpose of the this keyword in JavaScript?

A

this refers to the object that the function is a property of.
Its value can change depending on how the function is called.

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

How do you create an array in JavaScript?

A

let array = [element1, element2, element3];
let array = new Array(size); // Creates an array with the specified size

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

List some common array methods.

A

map(): Creates a new array by applying a function to each element of the original array.
filter(): Creates a new array with elements that pass a test provided by a function.
reduce(): Applies a function to an accumulator and each element in the array (from left to right) to reduce it to a single value.
forEach(): Executes a provided function once for each array element.

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

How do you iterate over an array in JavaScript?

A

for loop
for…of loop
forEach() method

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

How do you create an object in JavaScript?

A

let object = { property1: value1, property2: value2 };

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

How do you access and modify object properties?

A

Access:
object.property
object[‘property’]
Modify:
object.property = newValue
object[‘property’] = newValue

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

How do you select elements in the DOM using JavaScript?

A

getElementById()
querySelector()
querySelectorAll()

17
Q

How do you add, remove, and modify elements in the DOM?

A

Add:
createElement()
appendChild()
Remove:
removeChild()
Modify:
innerHTML
setAttribute()

18
Q

What are promises in JavaScript?

A

Promises represent the eventual completion (or failure) of an asynchronous operation and its resulting value.

19
Q

What is the difference between synchronous and asynchronous code?

A

Synchronous: Code executes line by line, waiting for each operation to complete before moving to the next.
Asynchronous: Code doesn’t wait for each operation to finish; it can continue executing other tasks while waiting for the asynchronous operation to complete.

20
Q

What are some common JavaScript debugging techniques?

A

Using the browser’s developer console (logging to the console, inspecting elements)
Setting breakpoints in the code
Using debugging tools (like Chrome DevTools) to step through code execution

21
Q

How do you handle errors in JavaScript?

A

try…catch…finally blocks:
try: Encloses the code that might throw an error.
catch: Executes if an error occurs within the try block.
finally: Executes regardless of whether an error occurred or not.

22
Q

What is a closure in JavaScript?

A

A closure is a function that has access to variables from the outer (enclosing) function’s scope, even after the outer function has finished executing.

23
Q

Explain the concept of prototypes in JavaScript.

A

Every object in JavaScript has a hidden link to a prototype object.
When you try to access a property on an object, and that property doesn’t exist, JavaScript looks for it on the prototype object.

24
Q

Write a JavaScript function that takes a string as input and returns the reverse of that string.

A

function reverseString(str) {
return str.split(‘’).reverse().join(‘’);
}

// Example Usage:
const inputString = “hello”;
const reversedString = reverseString(inputString);
console.log(reversedString); // Output: “olleh”

str.split(‘’): This splits the input string into an array of individual characters.
reverse(): This reverses the order of elements in the array.
join(‘’): This joins the elements of the reversed array back into a single string.

24
Q

Write a JavaScript function that takes an array of numbers as input and returns the sum of all the numbers in the array.

A

function sumArray(arr) {
return arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}

// Example Usage:
const numbers = [1, 2, 3, 4, 5];
const sum = sumArray(numbers);
console.log(sum); // Output: 15

arr.reduce(): This method iterates over the array and applies a reducing function to each element.
accumulator: The initial value (0) and the accumulated result of the previous iterations.
currentValue: The current element being processed in the iteration.
The reducing function adds currentValue to the accumulator in each iteration.

25
Q

Write a JavaScript function that takes a string as input and returns true if the string is a palindrome (reads the same forwards and backward), and false

A

function isPalindrome(str) {
const cleanStr = str.toLowerCase().replace(/[^a-zA-Z0-9]/g, ‘’);
return cleanStr === cleanStr.split(‘’).reverse().join(‘’);
}

// Example Usage:
const input1 = “racecar”;
const input2 = “A man a plan a canal Panama”;
console.log(isPalindrome(input1)); // Output: true
console.log(isPalindrome(input2)); // Output: true

str.toLowerCase(): Converts the string to lowercase.
replace(/[^a-zA-Z0-9]/g, ‘’): Removes any non-alphanumeric characters.
split(‘’).reverse().join(‘’): Reverses the cleaned string as in the previous “Reverse a String” example.
Compares the original cleaned string with the reversed string. If they are equal, it’s a palindrome.

26
Q

What are hogher order function in javascript

A
  • Take other functions as arguments: They can accept one or more functions as input parameters.
    // - Return other functions as output: They can produce new functions as their result.
27
Q

what are arrow functions and name a key difference between arrow functions and regular functions in javascript

A

this binding:
Regular functions: The this keyword within a regular function can have different values depending on how the function is called (e.g., as a method of an object, as a standalone function).
Arrow functions: Arrow functions do not have their own this. The this value inside an arrow function is inherited from the surrounding scope.

() => {} for functions with no parameters

(param1, param2) => {} for functions with multiple parameters

param => {} for functions with a single parameter

28
Q

FizzBuzz

A

for (let i = 1; i < 20; i += 1) {
if (i % 3 === 0 && i % 5 === 0) {
console.log(‘FixxBuzz’);
} else if (i % 3 === 0) {
console.log(‘fizz’);
} else if (i % 5 === 0) {
console.log(‘buzz’);
} else {
console.log(i);
}
}