JavaScript Basics Flashcards

JavaScript Basics (30 cards)

1
Q

What are the primitive data types in JavaScript?

A

The primitive data types in JavaScript are:
* String
* Number
* Boolean
* Null
* Undefined
* Symbol
* BigInt

None

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

Explain the difference between null and undefined.

A

Undefined means a variable has been declared but not assigned a value. Null is an explicit assignment to represent ‘no value’ or ‘empty’.

Undefined is a type itself, while null is an object (though this is considered a bug in JavaScript)

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

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

A

var is function-scoped and hoisted.
let is block-scoped, can be reassigned, but not redeclared in the same scope.
const is block-scoped, cannot be reassigned or redeclared, but object properties can still be modified.

None

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

What is variable hoisting?

A

Hoisting is JavaScript’s behavior of moving declarations to the top of their containing scope during compilation.

  • Function Declarations: Function declarations are fully hoisted, meaning you can call a function before it is defined in the code.
  • var are hoisted and initialized with undefined
  • let and const are hoisted but not initialized (temporal dead zone)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different ways to create a function in JavaScript?

A

Function declaration: function name() {};
Function expression: const name = function() {};
Arrow function: const name = () => {};
Method in object: const obj = { name() {} }.

None

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

What is a closure in JavaScript?

A

A closure is a function that has access to its own scope, the scope of the outer function, and global scope, even after the outer function has returned.

None

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

How do arrow functions differ from regular functions?

A
  • ‘This’ binding is from surrounding context
  • don’t have their own arguments object
  • can’t be used as constructors
  • have implicit return for one-liners

None

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

What is a callback function?

A

A callback function is a function passed as an argument to another function, which is then invoked inside the outer function to complete some action.

None

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

Explain the concept of ‘this’ in JavaScript.

A

‘this’ refers to the object that is executing the current function. Its value depends on how the function is called.

In methods, it refers to the owner object; in regular functions, it refers to the global object (or undefined in strict mode); in events, it refers to the element that received the event.

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

What are the ways to explicitly set the value of ‘this’?

A

You can explicitly set ‘this’ using:
* call()
* apply()
* bind()

None

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

What is the difference between synchronous and asynchronous code?

A

Synchronous code executes sequentially line by line, blocking further execution until the current operation completes. Asynchronous code allows operations to run in the background without blocking the main thread.

It uses callbacks, promises, or async/await to handle results.

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

Explain promises in JavaScript.

A

Promises are objects representing the eventual completion or failure of an asynchronous operation. They have three states:
* pending
* fulfilled (resolved)
* rejected

They use .then() for success handlers and .catch() for error handlers.

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

What is the difference between promises and async/await?

A

Async/await is syntactic sugar over promises, making asynchronous code look and behave more like synchronous code.

async functions always return promises, and await can only be used inside async functions to pause execution until a promise resolves.

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

Explain the event loop in JavaScript.

A

The event loop continuously checks if the call stack is empty. If empty, it takes the first task from the queue and pushes it onto the stack for execution.

This enables JavaScript’s non-blocking, asynchronous behavior despite being single-threaded.

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

What is the difference between map, filter, and reduce?

A

map() creates a new array by transforming each element.
filter() creates a new array with elements that pass a test function.
reduce() accumulates array elements into a single value based on a function with an accumulator.

None

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

How do you clone an object in JavaScript?

A
  • Object.assign({}, obj)
  • spread operator ({…obj}).
  • JSON.parse(JSON.stringify(obj)),
  • using libraries like Lodash’s cloneDeep, or the structured clone API.

Deep cloning has limitations.

17
Q

What is destructuring in JavaScript?

A

Destructuring is a syntax that allows you to extract multiple properties from arrays or objects into distinct variables:
* const {name, age} = person; for objects
* const [first, second] = array; for arrays.

None

18
Q

How do you check if a property exists in an object?

A
  1. Using the “in” operator (console.log(‘name’ in obj)
  2. Using the hasOwnProperty() method. (console.log(obj.hasOwnProperty(‘name’));)
  3. Check for “undefined”
  4. Use obj.keys() to return array of property names.

None

19
Q

What are template literals?

A

Template literals are string literals allowing embedded expressions and multi-line strings, denoted with backticks (``). They allow string interpolation with ${expression} syntax.

None

20
Q

What String methods would you use to transform ‘hello world’ to ‘Hello World’?

A

You can split the string, capitalize the first letter of each word, and join it back:
* ‘hello world’.split(‘ ‘).map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(‘ ‘).

None

21
Q

How would you check if a string contains a specific substring?

A

Using includes(): string.includes(substring)

None

22
Q

What are the benefits of using ES6 modules?

A

ES6 modules provide:
* Better organization
* Explicit imports/exports
* Better dependency management
* Tree Shaking. Eliminate unused code, smaller bundle
* Reusability
* Encapsulation
* Modularity

None

23
Q

What is the spread operator and how is it used?

A
  • It allows an iterable (like an array or an object) to be expanded into individual elements or properties.
  • It is commonly used for array manipulation, function arguments, and object cloning or merging.
  • Array concatenation
  • Copying arrays/objects
  • Spreading arguments in function calls
  • Gathering rest parameters

None

24
Q

What is the optional chaining operator (?.) and when would you use it?

A

Allows you to safely access deeply nested properties of an object without having to explicitly check if each reference in the chain is null or undefined.

  • const city = user.profile?.address?.city; // ‘Wonderland’
  • const zipCode = user.profile?.address?.zipCode; // undefined

It returns undefined if any reference is null or undefined instead of throwing an error.

25
How do you handle errors in JavaScript?
Using try/catch blocks for synchronous code, .catch() for promises, and try/catch with async/await for asynchronous code. ## Footnote You can also use the global error event or window.onerror handler.
26
What is the difference between throw and return when handling errors?
throw generates an exception that will terminate execution unless caught in a try/catch. return simply passes a value back from a function without interrupting execution flow. ## Footnote throw is used for exceptional conditions, while return is for normal program flow.
27
What is garbage collection in JavaScript?
Garbage collection is the process of automatically freeing memory that is no longer referenced by the program. ## Footnote JavaScript uses algorithms like mark-and-sweep to identify and remove unreachable objects.
28
How does JavaScript handle memory management?
JavaScript automatically allocates memory when objects are created and frees it when they're no longer referenced (garbage collection). ## Footnote Memory leaks can occur with circular references, forgotten timers/callbacks, or closures referencing large objects.
29
What are the most reliable resources for JavaScript documentation?
Reliable resources include: * MDN Web Docs (Mozilla Developer Network) * JavaScript.info * The official ECMAScript specification * W3Schools * Stack Overflow for community solutions ## Footnote None
30
If you encounter an unfamiliar JavaScript method, how would you research it?
Check MDN Web Docs for official documentation, search for examples on Stack Overflow, look for tutorials on developer blogs, or check the official documentation of the library/framework if it's not a native JavaScript method. ## Footnote None