Basic Data Types and Values
Let's look at some basic data types in JavaScript:
8.28 is a
number
"Hello" is a
string
They are values
, one of the fundamental things that a program manipulates.
Here we’ll briefly mention the basic data types in JavaScript. In the next chapters we’ll talk about them in details.
Number
The number type includes integer and floating point numbers.
String
A string may have one more more characters, it must be quoted by matching single quotes or double quotes.
Boolean
The boolean is a logical type has only two values: true
and false
.
All above types are called “primitive”, because their values can contain only a single thing (be it a string or a number or whatever).
The null
value
null
is a special value that represents the absence of any object value. It's one of JavaScript's primitive values.
The undefined
value
undefined
is another primitive value. If a variable has the value undefined
, it means the variable has been assigned a value yet.
Symbol
The symbol
type is not widely used in software so we will ignore it for now.
Object
In contrast, the object
type is for more complex data structures.
The typeof
operator
The typeof
operator returns the type of the argument(the input value), it's convenient to make a quick check.
It supports two forms of syntax:
As an operator: typeof x
Function style: typeof(x)
console.log(typeof 25); // number
console.log(typeof('Hello'); // string
Mixing Numbers and Strings
When you want to mix text and numbers in an expression, such as you like to show a message: "My age is 12." You can simply join the string text and the number using the +
operator. JavaScript is intelligent enough to know that when both string and number are involved, you are not trying to do math calculations, so the number is treated as a string and simply join it to the text.
console.log("My age is " + 808); // My age is 808