Fundamentals 14 min read

Ace JavaScript Interviews: Master Variables, Types, Memory & Garbage Collection

This article provides a comprehensive JavaScript guide covering primitive and reference types, Symbol and BigInt nuances, type‑checking operators, stack vs heap memory, deep and shallow copying techniques, and practical garbage‑collection strategies to help developers ace interview questions.

WeDoctor Frontend Technology
WeDoctor Frontend Technology
WeDoctor Frontend Technology
Ace JavaScript Interviews: Master Variables, Types, Memory & Garbage Collection

Preface

The author, a front‑end engineer, shares notes from reading the "JavaScript Advanced Programming" chapters on variables and memory, summarizing common interview questions and pitfalls.

Part 1: Variables

Basic Data Types

JavaScript defines eight primitive types: undefined, null, boolean, number, string, symbol, bigint, and object.

Symbol

Each call to Symbol() returns a unique value that can serve as an object property key.

The constructor new Symbol() is not allowed and throws a TypeError. Symbol('1') == Symbol('1') // false Q1: How to retrieve all Symbol‑based keys of an object? A: Object.getOwnPropertyNames, Object.getOwnPropertySymbols or Reflect.ownKeys.

Q2: typeof Symbol // "function", typeof Symbol() // "symbol".

BigInt

BigInt represents integers of arbitrary precision, useful for timestamps, large IDs, etc.

Number can safely represent integers only between -(2^53‑1) and 2^53‑1.

9007199254740991 // 9007199254740991
9007199254740992 // 9007199254740992
9007199254740993 // 9007199254740992 // loss of precision

Creating a BigInt: append n to an integer literal or use BigInt(). Comparison: 9n == 9 // true, 9n === 9 // false.

Reference Types

Objects can contain function, Array, Date, RegExp, etc.

null vs undefined

undefined

is the value of an uninitialized variable; null is an explicit empty object placeholder. Number(null) yields 0, while Number(undefined) yields NaN.

new Number(null) // Number {0}
new Number(undefined) // Number {NaN}

Part 2: Determining Data Types

typeof

Only works for primitive types (except null ). For null it returns "object". For objects, typeof returns "object" except for functions, which return "function".
var a;
console.log(a); // undefined
console.log(typeof a); // "undefined"

instanceof

Checks whether A is an instance of B; useful for known or custom constructors.
function Beauty(name, age) { this.name = name; this.age = age; }
var beauty = new Beauty('lnp', 18);
beauty instanceof Beauty // true

Examples show that [] instanceof Array is true, [] instanceof Object is also true, while literals cannot be checked with instanceof for Boolean, Number, String.

constructor

The constructor property returns the function that created the object. It cannot be used for undefined or null .

Object.prototype.toString.call()

Provides a reliable way to identify the internal [[Class]] of a value.
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(123); // "[object Number]"
Object.prototype.toString.call('lnp'); // "[object String]"
Object.prototype.toString.call(function(){}); // "[object Function]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call([1,2,3]); // "[object Array]"
Object.prototype.toString.call(/^[\d]{5,20}$/); // "[object RegExp]"

Part 3: Memory

Stack vs Heap

Primitive values are stored in stack memory; reference values are stored in heap memory with a pointer kept in the stack.

Deep vs Shallow Copy

Shallow copy duplicates only the pointer; deep copy recursively duplicates all nested structures.

Equality examples:

1 == 1 // true
[] == [] // false
{} == {} // false

Explanation of the assignment chain var a = {n:1}; var b = a; a.x = a = {n:2}; shows that a.x becomes undefined initially, then receives the new object, while b still references the original object with the added x property.

Copy Methods

JSON.parse(JSON.stringify(obj))

– works for plain JSON data.

postMessage, recursion, lodash – common deep‑copy techniques. Object.assign, spread operator, slice, Array.prototype.concat – shallow‑copy approaches.

Part 4: Garbage Collection

JavaScript provides automatic garbage collection using mark‑and‑sweep and reference‑counting. Developers should still avoid patterns that create leaks.

Memory Leak vs Stack Overflow

Leaks keep unreachable variables in memory; stack overflow occurs when the call stack grows beyond its limit. Accumulated leaks can eventually cause a stack overflow.

Common Leak Traps

Accidental global variables that are never reclaimed.

Timers that are not cleared.

References to DOM nodes that exist outside the DOM tree.

Closures that retain large scopes.

Leak Prevention Strategies

Minimize long‑lived globals and set them to null when done.

Avoid infinite loops and unnecessary object creation.

Limit deep reference chains.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory ManagementGarbage CollectionInterview Preparationdeep copytype checkingVariables
WeDoctor Frontend Technology
Written by

WeDoctor Frontend Technology

Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.