Primitive Data Types
While object types such as objects and arrays are mutable, primitive data types are immutable. Immutable data types are data types that cannot be changed once the value has been created.
Primitive data types include:
let x; console.log(x)
null is similar to
undefined in that it represents an unknown value, however, it is assigned to values that the programmer wishes to keep empty.
let x = null; console.log(x);
Let’s say that we are still working for the zoo. We have objects created for animals like so:
Now, a new giraffe is coming to the zoo. We may want to initialize an object for the giraffe, but hold off on storing information in the
weight property until the giraffe arrives.
In this case, we could initialize the
weight property like so:
This way, our object is properly initialized with all of the information we would need and we can update the
weight property later when we have accurate information.
Check Your Understanding
Which of the following are primitive data types? Mark ALL that apply.
Consider the following code block:
let x; console.log(x);
x is of what data type?