JavaScript Fundamentals Flashcards

Variables & Data Types Operators & Expressions Functions & Scope Hoisting & Closures (91 cards)

1
Q

What are variables in JavaScript?

A

Variables are containers for storing data values. They can be declared using var, let, or const.

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

What are the data types in JavaScript?

A

JavaScript has primitive types (Number, String, Boolean, null, undefined, Symbol, BigInt) and non-primitive types (Object, Array, Function).

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

What are the advantages of using ‘let’ and ‘const’ over ‘var’?

A

‘let’ and ‘const’ provide block scope, reducing errors due to variable hoisting and scope leakage associated with ‘var’.

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

What are the disadvantages of using ‘var’?

A

‘var’ is function-scoped and can lead to unexpected behavior due to hoisting and lack of block scope.

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

What is a best practice for declaring variables in JavaScript?

A

Use ‘const’ by default, and ‘let’ when reassignment is needed. Avoid using ‘var’ to prevent scope-related issues.

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

What is a use case for using ‘const’ in JavaScript?

A

Declaring constants or variables that should not be reassigned, such as configuration values.

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

How do variables impact system design in JavaScript?

A

Proper variable declaration and scoping lead to more predictable and maintainable code, reducing bugs and side effects.

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

What is an example of variable hoisting in JavaScript?

A

Accessing a ‘var’ declared variable before its declaration results in ‘undefined’ due to hoisting.

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

What are the architectural implications of improper variable scoping?

A

Can lead to tightly coupled code and unintended side effects, making maintenance and scaling difficult.

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

What is a performance consideration with variable declarations?

A

Using ‘let’ and ‘const’ can help prevent memory leaks by limiting the scope and lifespan of variables.

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

How can you monitor or debug variable-related issues?

A

Use debugging tools and linters to track variable declarations, scopes, and potential leaks.

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

What is a real-world tradeoff when choosing variable declarations?

A

Choosing between ‘let’ and ‘const’ for flexibility versus immutability and safety.

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

What is a common interview question about JavaScript variables?

A

Explain the difference between ‘var’, ‘let’, and ‘const’.

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

What is a potential gotcha with variable declarations?

A

Using ‘var’ can lead to unexpected behavior due to its function-scoped nature and hoisting.

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

What are operators in JavaScript?

A

Operators are symbols that perform operations on operands, such as addition (+), subtraction (-), and assignment (=).

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

What are expressions in JavaScript?

A

Expressions are combinations of values and operators that compute to a value.

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

What are the advantages of using strict equality (===) over loose equality (==)?

A

Strict equality checks for both value and type, reducing unexpected type coercion.

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

What are the disadvantages of using loose equality (==)?

A

Can lead to unexpected results due to type coercion.

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

What is a best practice when using operators in JavaScript?

A

Prefer strict equality (===) and avoid using ‘==’ to prevent type coercion issues.

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

What is a use case for using the ternary operator?

A

Simplifying conditional assignments: let result = condition ? value1 : value2;

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

How do operators impact system design in JavaScript?

A

Proper use of operators ensures accurate computations and logic flow, leading to reliable applications.

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

What is an example of using logical operators in JavaScript?

A

Using ‘&&’ to execute code only if multiple conditions are true.

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

What are the architectural implications of operator misuse?

A

Incorrect operator usage can lead to logic errors, affecting application behavior and reliability.

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

What is a performance consideration with complex expressions?

A

Overly complex expressions can be harder to read and maintain, potentially leading to performance issues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How can you monitor or debug operator-related issues?
Use debugging tools to step through expressions and evaluate their outcomes.
26
What is a real-world tradeoff when using shorthand operators?
Shorthand operators can make code concise but may reduce readability for complex operations.
27
What is a common interview question about JavaScript operators?
What is the difference between '==' and '===' in JavaScript?
28
What is a potential gotcha with operator precedence?
Misunderstanding operator precedence can lead to unexpected results in complex expressions.
29
30
What are functions in JavaScript?
Functions are reusable blocks of code that perform a specific task.
31
What is scope in JavaScript?
Scope determines the accessibility of variables and functions in different parts of the code.
32
What are the advantages of using functions?
Functions promote code reusability, modularity, and separation of concerns.
33
What are the disadvantages of improper scope management?
Can lead to variable collisions and unintended side effects.
34
What is a best practice for defining functions?
Use clear and descriptive names, and keep functions focused on a single task.
35
What is a use case for using closures?
Creating private variables and functions that are not accessible from the global scope.
36
How do functions and scope impact system design?
Proper function and scope management lead to modular, maintainable, and scalable codebases.
37
What is an example of a closure in JavaScript?
A function that returns another function, with access to variables in the outer function's scope.
38
What are the architectural implications of closures?
Closures enable encapsulation and data privacy, supporting object-oriented design patterns.
39
What is a performance consideration with closures?
Excessive use of closures can lead to increased memory usage due to retained references.
40
How can you monitor or debug scope-related issues?
Use debugging tools to inspect variable scopes and closures.
41
What is a real-world tradeoff when using closures?
Balancing data encapsulation with potential memory overhead.
42
What is a common interview question about JavaScript functions?
What is a closure, and how is it used in JavaScript?
43
What is a potential gotcha with function hoisting?
Function declarations are hoisted, but function expressions are not, leading to potential errors if called before definition.
44
45
What is hoisting in JavaScript?
Hoisting is JavaScript's behavior of moving declarations to the top of the current scope before code execution.
46
What are closures in JavaScript?
Closures are functions that retain access to their lexical scope even when executed outside of that scope.
47
What are the advantages of closures?
They enable data encapsulation and the creation of private variables.
48
What are the disadvantages of hoisting?
Can lead to confusion and bugs if developers assume variables are declared before use.
49
What is a best practice regarding hoisting?
Always declare variables and functions at the top of their scope to avoid unexpected behavior.
50
What is a use case for closures?
Implementing factory functions that generate customized functions with preserved state.
51
How do hoisting and closures impact system design?
Understanding these concepts is crucial for predictable code execution and memory management.
52
What is an example of hoisting in JavaScript?
Accessing a variable before its declaration results in 'undefined' due to hoisting.
53
What are the architectural implications of closures?
Closures support modular design by encapsulating functionality and state.
54
What is a performance consideration with closures?
Closures can increase memory usage if not managed properly due to retained references.
55
How can you monitor or debug hoisting issues?
Use linters and debugging tools to detect undeclared variables and unexpected behaviors.
56
What is a real-world tradeoff when using closures?
Balancing the benefits of data encapsulation with potential memory overhead.
57
What is a common interview question about hoisting?
Explain hoisting in JavaScript and how it affects variable and function declarations.
58
What is a potential gotcha with closures?
Unintended retention of variables leading to memory leaks.
59
What is a prototype in JavaScript?
A prototype is an object from which other objects inherit properties; it's the foundation of JavaScript's prototypal inheritance model.
60
What is the 'this' keyword in JavaScript?
The 'this' keyword refers to the object that is executing the current function; its value depends on how the function is called.
61
What are arrow functions in ES6?
Arrow functions are a concise syntax for writing functions using '=>' and do not bind their own 'this' context.
62
What is the spread operator (...) in JavaScript?
The spread operator expands an iterable (like an array or string) into individual elements.
63
What is the rest parameter syntax (...) in JavaScript?
The rest parameter collects multiple arguments into a single array, allowing for variable numbers of function arguments.
64
What is a key advantage of prototypes?
They allow memory-efficient method sharing across instances.
65
What is a disadvantage of prototypes?
It can be harder to trace behavior due to dynamic lookups and inheritance chains.
66
What is a key advantage of arrow functions?
They simplify function syntax and preserve 'this' from the enclosing scope.
67
What is a disadvantage of arrow functions?
They cannot be used as constructors and lack their own 'this', 'arguments', or 'super'.
68
What is a best practice when using 'this'?
Always understand function context; use arrow functions when you need lexical scoping of 'this'.
69
What is a best practice with prototypes?
Define shared methods on prototypes to optimize memory usage and improve performance.
70
What is a best practice with spread/rest operators?
Use spread for immutability (e.g., copying arrays), and rest for flexible APIs.
71
What is a use case for prototypes?
Creating reusable classes or constructors with shared behavior.
72
What is a use case for arrow functions?
Writing concise callbacks or preserving 'this' in methods like setTimeout or map.
73
What is a use case for the spread operator?
Copying or merging arrays and objects immutably.
74
What is a use case for the rest parameter?
Creating functions that accept variable numbers of arguments, like a sum() function.
75
How do prototypes impact system design?
They enable efficient object-oriented patterns and reduce memory footprint across many instances.
76
How does 'this' affect architectural decisions?
Incorrect use of 'this' can introduce subtle bugs and require design patterns like bind(), closures, or arrow functions.
77
How do ES6+ features affect design?
They promote cleaner, more readable code with fewer bugs, especially in asynchronous and functional patterns.
78
How do prototypes affect performance?
Prototypes offer better memory usage, as methods are not redefined per object instance.
79
Do arrow functions impact fault tolerance?
Improper use can lead to unexpected 'this' behavior, especially in class methods, reducing reliability.
80
How can you debug issues with 'this'?
Use console logs to inspect 'this', or use strict mode to catch undefined contexts.
81
How to monitor performance with prototypes?
Use profiling tools like Chrome DevTools to check object creation patterns and prototype chains.
82
What is a real-world tradeoff with arrow functions?
They simplify syntax but may be misused in situations where traditional functions are more appropriate (e.g., when a dynamic 'this' is needed).
83
What is a tradeoff with rest/spread operators?
They provide clarity but can introduce performance overhead in large data manipulations.
84
What is a tradeoff with prototypes?
More efficient memory usage vs. more complexity in debugging inheritance issues.
85
Interview Question: How does JavaScript's prototype chain work?
Each object has an internal [[Prototype]] link to another object; this chain is followed for property lookups.
86
Interview Question: What is the difference between arrow functions and regular functions?
Arrow functions don't bind their own 'this', cannot be constructors, and have concise syntax.
87
Interview Question: How can you handle 'this' in asynchronous code?
Use arrow functions, bind(), or store a reference like 'const self = this'.
88
Interview Question: How do you merge two objects in JavaScript?
Use the spread operator: {...obj1, ...obj2}.
89
What is a common gotcha with arrow functions?
They can't be used as methods when dynamic 'this' is required; 'this' will be inherited lexically.
90
What is a gotcha with prototypes?
Modifying a prototype after object instantiation may not reflect correctly or cause unexpected behavior.
91
What is a gotcha with rest/spread?
Shallow copy behavior can lead to bugs if objects contain nested references.