Data Types in Javascript
JavaScript is a dynamically typed language.
This means that you don't have to specify the data type of a variable when you declare it.
The data type of the variable is determined automatically when the program is being processed.
javascript data types are divided into two types:
- Primitive data types
- Non primitive data types
Primitive data types
- undefined: represents a variable that has not been assigned a value.
- null: represents a deliberate non-value.
- boolean: represents a logical value
true
orfalse
. - number: represents a numerical value.
- string: represents a sequence of characters.
As a example:
let myVariable; // undefined
let myNullVariable = null; // null
let myBooleanVariable = true; // boolean
let myNumberVariable = 5; // number
let myStringVariable = "Hello"; // string
Non primitive data types
- object: represents a collection of related data and/or functionality.
As an example:
let myObject = {}; // empty object
let myArray = []; // empty array (which is also an object)
let myFunction = function () {}; // function (which is also an object)
Object data type
object represents a collection of related data and/or functionality.
As an example:
let myObject = {}; // empty object
let myArray = []; // empty array (which is also an object)
let myFunction = function () {}; // function (which is also an object)
Special data types
symbol represents a unique identifier, used primarily for object property keys.
As an example:
let mySymbol = Symbol(); // symbol
Knowing JavaScript's data types is key to writing code that properly manages and modifies data.
note
JavaScript is dynamically typed, meaning it is not mandatory to declare variable data types. Instead, the data type is determined at runtime based on the assigned value.