Lesson 19-Node Interview Knowledge Points

JavaScript Basics

Type Checking

JavaScript has 7 primitive types (undefined, null, boolean, number, string, symbol, bigint) and object types (object). Type checking methods include:

  • typeof: Checks primitive types (note: typeof null returns "object").
  • instanceof: Checks if an object is an instance of a constructor.
  • Object.prototype.toString.call(): Precisely identifies types.

Example

typeof 42; // "number"
typeof null; // "object"
[] instanceof Array; // true
Object.prototype.toString.call([]); // "[object Array]"

Scope

JavaScript supports global, function, and block-level scopes (ES6 let and const). The scope chain is determined by the lexical environment.

Example

let x = 1;
function foo() {
  let x = 2;
  console.log(x); // 2
}
foo();
console.log(x); // 1

Pass by Reference

In JavaScript, primitive types are passed by value, while object types (including arrays and functions) are passed by reference.

Memory Management

JavaScript uses garbage collection (primarily mark-and-sweep). Variables are automatically reclaimed by the V8 engine when they lose references.

ES6 Features

  • let and const: Block-level scoping.
  • Arrow Functions: Concise syntax, no own this.
  • Template Literals: Support interpolation.
  • Destructuring Assignment: Extract values from arrays/objects.
  • Promises: Handle asynchronous operations.
  • Modules: import and export.

Example

const [a, b] = [1, 2];
const greet = name => `Hello, ${name}`;

Common Questions and Answers

1. Which types in JavaScript are passed by reference, and which by value? How to pass a value type by reference?

  • Answer:
  • Pass by Value: Primitive types (number, string, boolean, undefined, null, symbol, bigint). Assignment copies the value, and modifications don’t affect the original.
  • Pass by Reference: Object types (object, array, function). Assignment passes the reference, and modifications affect the original.
  • Pass Value by Reference: Wrap the value in an object.
  • Example:
let num = 10;
let obj = { value: num };
function modify(ref) {
  ref.value = 20;
}
modify(obj);
console.log(obj.value); // 20

2. Is 0.1 + 0.2 === 0.3 true in JavaScript? How to check if the sum of two floating-point numbers equals a third number without knowing their precision?

  • Answer:
  • Result: false, because JavaScript uses IEEE 754 double-precision floating-point, so 0.1 + 0.2 yields 0.30000000000000004.
  • Method: Compare within an error margin (e.g., Number.EPSILON) or convert to integers.
  • Example:
console.log(0.1 + 0.2 === 0.3); // false
function areEqual(a, b, c) {
  return Math.abs((a + b) - c) < Number.EPSILON;
}
console.log(areEqual(0.1, 0.2, 0.3)); // true

3. Can elements in an Array defined with const be modified? If so, what’s the purpose of const for objects?

  • Answer:
  • Modification: Yes, const only prevents reassigning the variable, not modifying object contents.
  • Purpose: Prevents reassignment, improving code predictability.
  • Example:
const arr = [1, 2, 3];
arr[1] = 4; // Allowed
console.log(arr); // [1, 4, 3]
arr = [5, 6, 7]; // Error: Assignment to constant variable

4. When is memory for variables of different types and environments released in JavaScript?

  • Answer:
  • Primitive Types: Stored in the stack, released when the scope ends.
  • Object Types: Stored in the heap, released by the garbage collector when no longer referenced (mark-and-sweep).
  • Environments:
    • Browser: Global variables are released when the page closes; local variables when the scope ends.
    • Node.js: Global variables are released when the process ends.
  • Example:
function test() {
  let obj = { a: 1 };
  return () => obj = null; // Manually dereference
}
const release = test();
release(); // obj can be garbage-collected

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here

Share your love