JavaScript is often said to be untyped, but that’s not entirely accurate. While it is true that JavaScript allows you to assign different types of values to variables, it does have types. In fact, JavaScript provides both primitive types and object types.
Let’s take a closer look at the different types in JavaScript:
Primitive types
JavaScript has several primitive types:
- Number: All numbers in JavaScript are internally represented as floats.
- String: A sequence of characters enclosed in quotes.
- Boolean: A type that represents either true or false.
- Symbol: A type introduced in ES2015 that represents unique values.
In addition to these types, JavaScript also has two special types:
- null: A value that represents the absence of any object value.
- undefined: A value that indicates an uninitialized variable or a missing property.
Now, let’s dive into each of these types in more detail.
Numbers
In JavaScript, all numbers are represented as floats. You can define numbers as either integer literals or floating-point literals. For example:
10
5354576767321
0xCC // hexadecimal
3.14
.1234
5.2e4 // 5.2 * 10^4
Strings
Strings represent sequences of characters and can be defined using single quotes or double quotes. They can also span multiple lines using the backslash character. Here are some examples:
'A string'
"Another string"
"A \
string"
'I\'m a developer'
Strings can be concatenated using the +
operator. They can also be defined using template literals, introduced in ES2015, which allow for more powerful string definition and string substitution. For example:
const a_string = `something`
`a string with ${something}`
`a string with ${something + somethingElse}`
`a string with ${obj.something()}`
Booleans
JavaScript has reserved words for boolean values: true and false. Various comparison operations return boolean values, and conditionals and control structures use booleans to determine program flow. JavaScript also has truthy and falsy values. Falsy values are values interpreted as false, such as 0, NaN, undefined, null, and an empty string. Any other value is considered truthy.
null
The value null represents the absence of any object value. It is commonly used to indicate that a variable has no value. Other languages may use similar concepts, such as nil
in Python.
undefined
The value undefined indicates that a variable has not been initialized and is absent. It is often returned by functions that have no explicit return value. When a function accepts a parameter that is not set by the caller, it defaults to undefined.
Object types
Any value that is not a primitive type in JavaScript is an object type. Object types have properties and methods that can act on those properties.
Checking the type of a variable
To find out the type of a variable in JavaScript, you can use the typeof
operator. It returns a string representation of the type. Here are some examples:
typeof 1 === 'number'
typeof '1' === 'string'
typeof {name: 'Flavio'} === 'object'
typeof [1, 2, 3] === 'object'
typeof true === 'boolean'
typeof undefined === 'undefined'
typeof (() => {}) === 'function'
It is worth noting that JavaScript does not have a separate function
type. The typeof
operator conveniently returns “function” for a function value.
Now that you have a better understanding of JavaScript types, you can use them effectively in your code. Keep in mind that understanding types can help you write more robust and bug-free JavaScript programs.