JavaScript - Data Types
JavaScript - Data Types
JavaScript Data Types
Data types in JavaScript referes to the types of the values that we are storing or working with. One of the most fundamental characteristics of a programming language is the set ofdata typesit supports. These are the type of values that can be represented and manipulated in a programming language.
JavaScript data types can be categorized as primitive and non-primitive (object). JavaScript (ES6 and higher) allows you to work with sevenprimitive data typesβ
- Stringsof text e.g. βThis text stringβ etc.
- Numbers, eg. 123, 120.50 etc.
- Booleane.g. true or false.
- null
- undefined
- BigInt
- Symbol
BigIntandSymbolare introduced in ES6. In ES5, there were only five primitive data types.
In addition to these primitive data types, JavaScript supports acomposite data typeknown as object. We will cover objects in detail in a separate chapter.
The Object data type contains the 3 sub-data types β
- Object
- Array
- Date
Why are data types important?
In any programming language, data types are important for operation manipulation.
For example, the below code generates the 1010 output.
let sum = "10" + 10;Here, the JavaScript engine converts the second operand to a string and combines it using the β+β operator rather than adding them.
So, you need to ensure that the type of operands is correct.
Now, letβs learn about each data type with examples.
JavaScript String
In JavaScript, the string is a sequence of characters and can be created using 3 different ways given below β
- Using the single quote
- Using the double quote
- Using the backticks
Example
In the example below, we have created strings using single quotes, double quotes, and backticks. In the output, it prints the same result for all 3 strings.
JavaScript Number
A JavaScript number is always stored as afloating-pointvalue (decimal number).
JavaScript does not make a distinction betweenintegervalues andfloating-pointvalues.
JavaScript represents numbers using the64-bitfloating-point format defined by the IEEE 754 standard.
Example
In the example below, we demonstrate JavaScript numbers with and without decimal points.
Example (Exponential notation of numbers)
JavaScript also support exponential notaion of numbers. We have explained this in the below example code β
JavaScript Boolean
In JavaScript, theBooleandata type has only two values:trueorfalse.
JavaScript Undefined
When you declare a variable but donβt initialize it, it contains anundefinedvalue. However, you can manually assign an undefined value to the variable also.
JavaScript Null
When any variableβs value is unknown, you can use thenull. It is good practice to use thenullfor the empty or unknown value rather than the undefined one.
JavaScript Bigint
JavaScript stores only 64-bit long floating point numbers. If you want to store a very large number, you should use theBigint. You can create Bigint by appending n to the end of the number.
JavaScript Symbol
The Symbol data type is introduced in the ES6 version of JavaScript. It is used to create unique primitive, and immutable values.
TheSymbol()constructor can be used to create a unique symbol, and you may pass the string as a parameter of the Symbol() constructor.
Example
In the example below, we created thesym1andsym2symbols for the same string. After that, we compared the value ofsym1andsym2, and it gave a false output. It means both symbols are unique.
JavaScript Object
In JavaScript, theobjectdata type allows us to store the collection of the data in thekey-valueformat. There are multiple ways to define the object, which we will see in the Objects chapter.
Here, we will create an object using the object literals.
Example
In the example below, we used the β{}β (Object literals) to create an obj object. The object contains the βanimalβ property with the string value, the βlegsβ property with the number value, and the value of the βcolorβ variable is assigned to the βhourseColorβ property.
The JSON.stringify() method converts the object to strings and shows it in the output.
JavaScript Array
In JavaScript, thearrayis a list of elements of the different data types. You can create an array using two square brackets β[]β and insert multiple comma seprated values inside the array.
JavaScript Date
You can use the JavaScriptDate objectto manipulate the date.
Example
In the example below, we used theDate()constructor to create a date. In the output, you can see the current date and time according to your time zone.
Dynamic Types
JavaScript is adynamically typedlanguage like Python and Ruby. So, it decides the variableβs data type at the runtime but not at the compile time. We can initialize or reassign the value of any data type to the JavaScript variables.
Example
In the example below, we initialized the first variable with the string value. After that, we updated its values to the number and boolean value.
Checking Data Types Using the typeof Operator
Thetypeofoperator allows you to check the type of the variable.
Example
In the below example, we used the typeof operator to check the data type of the various variables.