Javascript Data Types by Dan
Javascript Data Types by Dan
Primitive Values
Primitive Values are numbers and strings, among other things. Open your
console.log(2);
console.log("hello");
console.log(undefined);
All primitive values have something in common. There’s nothing I can do in my
code that would affect them. This sounds a bit vague, so we’ll explore what
this means concretely in the next module. For now, I’ll say that primitive values
are like stars — cold and distant, but always there when I need them.
them very special. Go ahead and log a few of them to the browser console:
console.log({});
console.log([]);
console.log(x => x * 2);
Notice how the browser console displays them differently from the primitive
special when you click them. If you have a few different browsers installed (e.g.
Chrome and Firefox), compare how they visualize objects and functions.
Objects and functions are special because I can manipulate them from my
code. For example, I can connect them to other values. This is rather vague —
so we’ll refine this idea in a later module. For now, I can say that if primitive
values are like distant stars, then objects and functions are more like rocks
floating nearby my code. They’re close enough that I can manipulate them.
You might have questions. Good. If you ask a question, the JavaScript universe
might answer it! Provided, of course, that you know how to ask.
Expressions
There are many questions JavaScript can’t answer. If you want to know
whether it’s better to confess your true feelings to your best friend or to keep
waiting until you both turn into skeletons, JavaScript won’t be of much help.
If we “ask” the expression 2 + 2, JavaScript will “answer” with the value 4.
expresses a value. You might hear people say that 2 + 2 “results in” or
“evaluates to” 4. These are all different ways to say the same thing.
I previously said that there are many types of JavaScript values: numbers,
strings, objects, and so on. How do we know any particular value’s type?
in the sky. But if you look closely, you’ll realize there are fewer than ten different
If we want to check a value’s type, we can ask it with the typeof operator.
JavaScript will answer our question with one of the predetermined string
Below are a few examples you can try in the browser console:
Strictly saying, using parens isn’t required with typeof. For example, typeof 2
would work just as fine as typeof(2). However, sometimes parens are required
to avoid an ambiguity. One of the cases below would break if we omitted the
Now take another look at the last three examples — this time with close
attention to their results. Did you find any of these results surprising? Why?
Types of Values
As an aspiring astronomer, you might want to know about e
very type of value
that can be observed in the JavaScript sky. After almost twenty five years of
studying JavaScript, the scientists have only discovered nine such types:
Primitive Values
● Undefined (undefined), used for unintentionally missing values.
● Null (null), used for intentionally missing values.
● Booleans (true and false), used for logical operations.
● Numbers (-100, 3.14, and others), used for math calculations.
● Strings ("hello", "abracadabra", and others), used for text.
● Symbols (uncommon), used to hide implementation details.
● BigInts (uncommon and new), used for math on big numbers.
● Objects ({} and others), used to group related data and code.
● Functions (x => x * 2 and others), used to refer to code.
No Other Types
You might ask: “But what about other types I have used, like arrays?”
In JavaScript, there are no other fundamental value types other than the ones
we have just enumerated. The rest are all objects! For example, even arrays,
console.log(typeof([])); // "object"
console.log(typeof(new Date())); // "object"
console.log(typeof(/(hello|goodbye)/)); //
"object"
“I see,” you might reply, “this is because everything is an object!” Alas, this is a
popular urban legend, but it’s not true. Although code like "hi".toUpperCase()
makes "hi" seem like an object, this is nothing but an illusion. JavaScript
creates a wrapper object when you do this, and then immediately discards it.
Recap
Let’s recap what we know so far:
1. There are values, and then there’s everything else. We can think of
values as different things “floating” in our JavaScript universe. They
don’t exist inside our code, but we can refer to them from our code.
2. There are two categories of values: there are Primitive Values, and
then there are O bjects and Functions. In total, there are nine separate
types. Each type serves a specific purpose, but some are rarely used.
3. Some values are lonely. For example, null is the only value of the Null
type, and undefined is the only value of the Undefined type. As we will
learn later, these two lonely values are quite the troublemakers!
4. We can ask questions with expressions. JavaScript will answer to us
with values. For example, the 2 + 2 expression is answered with 4.
5. We can inspect the type of something by wrapping it in a typeof
expression. For example, typeof(4) is the string value "number".