Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 04 / 34

Data types & value references

01

Understand the idea

You copy a settings , change the copy, and the original changes too. The assignment copied a reference, not a second object.

What assignment copies

A type tells you what kind of value you have and which operations make sense for it. The text "4" and the number 4 may look similar on a screen, but adding one does different things to them. In JavaScript the value has the type; a let can later hold a value of another type.

Meet the seven primitive types

Strings hold text, numbers hold numeric values, and booleans hold true or false. BigInt handles whole numbers beyond the safe integer range of Number; it does not hold decimals, and you cannot freely mix BigInt and Number in arithmetic. A Symbol creates a unique identifier, often used as an object key. commonly means no value was supplied; null is often an explicit choice to represent absence. NaN is a special Number value for an invalid numeric result, not an eighth type.

Immutable means the value itself cannot be changed in place. Reassigning a name from one number to another creates no contradiction: you have chosen a different value. Strings are immutable too. An operation that changes text produces another string; it does not edit the original character sequence. The table lists every primitive, including the two that you will probably use less often at first: BigInt and Symbol.

All seven JavaScript primitive types
Primitive typeExampletypeof resultMutable / Immutable
String"hello""string"Immutable
Number42 or 3.5"number"Immutable
BigInt42n"bigint"Immutable
Booleantrue or false"boolean"Immutable
Undefinedundefined"undefined"Immutable
Nullnull"object" (historical exception)Immutable
SymbolSymbol("id")"symbol"Immutable

Objects can be shared

An object groups named , such as sound and theme. A reference gives your code access to one particular object. Assignment copies that reference value, so two bindings can lead to the same object. This is still passing values, not giving the access to reassign the caller’s .

Remember that and functions are objects too, although typeof reports "function" for callable functions. People sometimes call objects reference types: copying the value gives another way to reach the same object. It does not give the second variable control over the first variable's binding. Reassigning one binding still leaves the other alone.

Comparisons need a rule

A comparison asks whether two values match under a particular rule. JavaScript does not choose that rule from how similar the values look on screen. Start by checking the types on both sides, then decide whether you want matching values or the same object.

Strict equality compares primitives without converting their types, so 4 === "4" is false. Loose equality can convert types, making 4 == "4" true. Two separately created objects are different identities even when their match. These are reasons to inspect your values, not reasons to memorize a list of surprising puzzles.

Choose the right copy

Use typeof as a quick inspection tool, with two checks worth keeping nearby: value === null tests for null, and Array.isArray(value) tests specifically for an array. Knowing these exceptions keeps a broad type check from turning into a false about a value’s contents.

In the example, changing copiedScore leaves score at 4. Changing sharedSettings.sound is visible through settings because both names reach the same object. A spread copy gives you a new outer object, but nested objects are still shared. Choose which levels need independence before editing a draft of saved data.

02

Mental model

FOLLOW THE VALUE

score → 4
copy primitive
copiedScore → 4

Each binding holds a primitive value.

01 / 03Trace the transformation

03

Live example

Changing copiedScore does not change score. Changing the shared does change what settings.sound reads.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Identify a shared reference

Make sharedSettings refer to the same object as settings so changing sound is visible through both names.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

4
false

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does assigning an object copy?
    02Which check identifies an array?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson