Data Types in JavaScript
What is a Data Type?
In JavaScript, everything is data except:
Operators: +
, !
, <=
, etc…
Reserved words: functions
, for
, debugger
, etc.
In JavaScript, every kind of data falls into JavaScript’s seven data types which include numbers, strings, booleans, symbols, objects, null and undefined
How to identify a data type?
One way to find out what type of data type we are dealing with is typeof
operator.
See below:
Numbers —
In Javascript, we have a single one encompassing number type, in other languages divide the numbers into integers, decimals, doubles and floats to have higher precision.
NOTE*Keep in mind that as JavaScript has become a language for the backend and front end, because of its imprecision for numbers, keeps it from development in banking or engineering applications where precision is vital.
Strings —
Strings are used to represent text in JavaScript. Strings are represented by using 'single-quotes'
, "double-quotes"
, `backticks`
with zero or more characters inside.
Booleans —
Boolean returns of two possible values: true or false
. Booleans are important and play a big role in if
statements in JavaScript.
Objects —
In JavaScript, objects are a collection of properties in curly braces {}
. The properties point to values of any data type — they can even be objects. Example below:
null and undefined —
The null
data type falls under an intentionally absent object. Undefined
usually means that something has not yet been assigned a value
Conclusion:
Knowing your data types is important because if you're aware of what data type you're working with makes it easier to determine what values you can assign to it and what can you do with the data type.