JavaScript data (primitives)
the building blocks of web apps
We can categorize basic forms of data, known as primitives, as follows:
Number
: a value we can calculate with another, e.g.an integer like
3
,-1
or0
a floating point number like
-273.15
String
: a string of alphanumeric charactersplain text like
String
orHello world!
plain text that has non-calculatable numbers like
Price: $200
Boolean
: a value that is either true or falsenull
: an empty valueundefined
: a variable with nothing assigned (not evennull
)Object
: a value with a special structure that organizes a set of properties and their values for a group of similar thingsSymbol
: (beyond the scope of this introduction)
Example of null
and undefined
null
and undefined
Note that 0
does not mean the same thing as null
in JavaScript:
0
represents a number with which we can do calculationsnull
represents the intentional absence of a numberundefined
represents that we have not yet decided on a value or anull
typeof
keyword
typeof
keywordTo see what the type of value a variable has, we use typeof
before a variable or value:
Copying by value
When we copy some primitives, we copy by value:
Note that despite setting y
equal to x
, then changing y
's value - the value of x
does not change!
This happens with:
a
Number
a
String
a
Boolean
a
null
an
undefined
a
Symbol
An analogy of copying by value: copying an image from the internet and then modifying the copy in an image editing software; the original image does not change!
Copying by reference
However, with:
an
Object
a
function
an
Array
...we copy by reference:
Notice how changing object2
's property also changed object1
's property automatically!
An analogy of copying by reference: editing a post on social media (our edit results in changing the original for all to see, as we change the original data!)
Last updated