CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

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.

JAVASCRIPT CODE
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.

Preview

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.

Preview

Example (Exponential notation of numbers)

JavaScript also support exponential notaion of numbers. We have explained this in the below example code βˆ’

Preview

JavaScript Boolean

In JavaScript, theBooleandata type has only two values:trueorfalse.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview

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.

Preview