JavaScript Types
You might sometimes read that JS is untyped, but that's incorrect. It's true that you can assign all sorts of different types to a variable, but JavaScript has types. In particular, it provides primitive types, and object types.
- Primitive types
- Numbers
- Strings
- Booleans
- null
- undefined
- Object types
- How to find the type of a variable
Primitive types
Primitive types are
And two special types:
- null
- undefined
Let’s see them in detail in the next sections.
Numbers
Internally, JavaScript has just one type for numbers: every number is a float.
A numeric literal is a number represented in the source code, and depending on how it’s written, it can be an integer literal or a floating point literal.
Integers:
10
5354576767321
0xCC //hexFloats:
3.14
.1234
5.2e4 //5.2 * 10^4Strings
A string type is a sequence of characters. It’s defined in the source code as a string literal, which is enclosed in quotes or double quotes
'A string'
"Another string"Strings can span across multiple lines by using the backslash
"A \
string"A string can contain escape sequences that can be interpreted when the string is printed, like \n to create a new line. The backslash is also useful when you need to enter for example a quote in a string enclosed in quotes, to prevent the char to be interpreted as a closing quote:
'I\'m a developer'Strings can be joined using the + operator:
"A " + "string"Template literals
Introduced in ES2015, template literals are string literals that allow a more powerful way to define strings.
const a_string = `something`You can perform string substitution, embedding the result of any JS expression:
`a string with ${something}`
`a string with ${something+somethingElse}`
`a string with ${obj.something()}`You can have multiline strings easily:
`a string
with
${something}`Booleans
JavaScript defines two reserved words for booleans: true and false.
Many comparision operations == === < > (and so on) return either one or the other.
if, while statements and other control structures use booleans to determine the flow of the program.
They don’t just accept true or false, but also accept truthy and falsy values.
Falsy values, values interpreted as false, are
0
-0
NaN
undefined
null
'' //empty stringAll the rest is considered a truthy value.
null
null is a special value that indicates the absence of a value.
It’s a common concept in other languages as well, can be known as nil or None in Python for example.
undefined
undefined indicates that a variable has not been initialized and the value is absent.
It’s commonly returned by functions with no return value.
When a function accepts a parameter but that’s not set by the caller, it’s undefined.
To detect if a value is undefined, you use the construct:
typeof variable === 'undefined'Object types
Anything that’s not a primitive type is an object type.
Object types have properties and also have methods that can act on those properties.
How to find the type of a variable
Any variable has a type assigned. Use the typeof operator to get a string representation of a type:
typeof 1 === 'number'
typeof '1' === 'string'
typeof {name: 'Flavio'} === 'object'
typeof [1, 2, 3] === 'object'
typeof true === 'boolean'
typeof undefined === 'undefined'
typeof (() => {}) === 'function'Why typeof returned “function”? JavaScript has no function type.
That’s true, and that’s a quirk of typeof which conveniently returns that value.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.