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

.

<code>Symbol('1') == Symbol('1') // false</code>

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.

<code>9007199254740991 // 9007199254740991
9007199254740992 // 9007199254740992
9007199254740993 // 9007199254740992 // loss of precision</code>

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

.

<code>new Number(null) // Number {0}
new Number(undefined) // Number {NaN}</code>

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".
<code>var a;
console.log(a); // undefined
console.log(typeof a); // "undefined"</code>

instanceof

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

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.
<code>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]"</code>

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:

<code>1 == 1 // true
[] == [] // false
{} == {} // false</code>

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.

JavaScriptmemory 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

login 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.