JavaScript Data Types & Data Structure
JavaScript Data Types
In JavaScript, data types specifies the kind of values stored in variables.
JavaScript has a dynamic typing system. It means that variables can hold values of any data type.
JavaScript is a weakly typed language. This means that variables do not have explicitly declared data types, and the interpreter determines the type of a value at runtime based on its usage.
There are two main categories of JavaScript data types: primitive and complex data types.
Understanding data types is crucial in programming. It is essential to be aware of the data types to write correct and efficient code.
Primitive Data Types:
Primitive data types are immutable which means their values cannot be changed once assigned.
-
Number : This represents both integers and floating-point numbers.
-
BigInt : This represents large integers that are too large to be stored in the standard 'number' type.
-
String : This represents a sequence of characters (text). It is used to store textual data. Strings are written within single or double quotes.
-
Boolean : This represents logical values: true or false.
-
Undefined : This represents a variable which is declared but no value is assigned to it.
-
Null : This represents the absence of a value.
-
Symbol :: This is a unique and immutable primitive data type used to create unique object keys.
Complex Data Types:
Complex data types are mutable which means their properties can be modified after creation.
-
Object as Data Type : Object is a collection of key-value pairs, where keys are string data type and values can be of any data type.
-
Array as Data Type : Array is a special type of object that can store an ordered list of values.
-
Function as Data Type : Functions are first-class objects. They can be assigned to variables, passed as arguments, or returned from other functions.
-
Date as Data Type : A Date is an object that represents a specific point in time.
-
Set as Data Type : A Set is a collection of unique values. Duplicate values are automatically discarded in set.
-
Map as Data Type : A Map is a collection of key-value pairs. In Map, keys can be of any data type.
JavaScript Data Structure
In JavaScript, data structure is a way of organizing and storing data for efficient access and modification.
-
Object as Data Structure : In JavaScript, an object is a collection of key-value pairs. Object's keys (properties) are strings or symbols, and the values can be any type. Object is a versatile and flexible data structure that allow to store and organize information in a flexible manner.
-
Array as Data Structure : An array is a collection of elements. Each element has an index and the element can be accessed using its index. Array supports common methods like push(), pop(), shift(), and unshift().
-
Function as Data Structure : Functions can be used as objects. They can hold properties just like any other object.
-
Date as Data Structure : A Date is an object that is used to represent and manipulate dates and times. The Date object provides many methods to perform different tasks with the year, month, day, etc. of a date.
-
Set as Data Structure : In JavaScript, a Set is a built-in data structure. A Set is a collection of unique values. Duplicate values are automatically discarded. Set has methods to manipulate elements of the set.
-
Map as Data Structure : In Javascript, a Map is another built-in data structure which is used to store a collection of key-value pairs. In Map, keys can be of any data type. Map's methods like set(), get(), has(), and delete() allow to manipulate the key-value pairs.
JavaScript Compiler For Live Practice
Practice the above JavaScript code in the following JavaScript Editor. Try more JavaScript data types and data structure code in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.
JavaScript Editor