DataTypes in javaScript
data types are used to categorize the different kinds of values that can be stored and manipulated in a program. JavaScript has two categories of data types: primitive types and object types.
Primitive types: These are the most basic data types in JavaScript, and they are immutable (cannot be changed). There are six primitive data types in JavaScript:
Number: Used for numeric values, including integers and floating-point numbers. Examples of number literals in JavaScript include
42
,3.14
, and-10
.String: Used for textual data. Strings are enclosed in quotes (either single or double quotes) and can contain any combination of letters, numbers, symbols, and spaces. Examples of string literals in JavaScript include
"hello"
,'world'
, and"42"
Boolean: Used for logical values. There are only two possible values:
true
andfalse
. Boolean values are often used in conditional statements and logical operations. Examples of boolean literals in JavaScript includetrue
andfalse
.Null: Used to represent the intentional absence of any object value. In JavaScript,
null
is a special keyword that represents a null value. It is often used to indicate that a variable or object does not have a value or reference. For example,let x = null;
creates a variablex
with a value of null.Undefined: Used to represent the absence of a value or the value of an uninitialized variable. In JavaScript,
undefined
is a special keyword that represents a value that has not been assigned. For example,let x;
creates a variablex
with a value of undefined.Symbol: Introduced in ECMAScript 6, symbols are unique and immutable data types that can be used as property keys on objects. Symbols are often used to define private properties and methods in JavaScript. Symbols are created using the
Symbol()
function. For example,const mySymbol = Symbol();
creates a symbolmySymbol
with a unique value.
Object types: These are more complex data types that can store collections of data, including other objects and functions. Objects in JavaScript are mutable (can be changed). There are several object data types in JavaScript, including:
Object: Objects are used in JavaScript to store collections of data. They are created using curly braces {} and can have properties and methods. Properties are key-value pairs that represent the data stored in an object, and methods are functions that can be called on the object.
Array: Arrays are used to store ordered collections of data. They are created using square brackets [] and can contain any type of data, including other arrays and objects. Arrays are zero-indexed, meaning that the first item in an array has an index of 0.
Function: Functions are used to store a block of code that can be executed when called. They are created using the function keyword and can have parameters and return values. Functions can be assigned to variables, passed as arguments to other functions, and returned as values from functions.
Date: The Date object is used to store date and time information in JavaScript. It provides methods for creating and manipulating dates and times, as well as for formatting and displaying them.
RegExp: The RegExp object is used to store regular expressions in JavaScript. Regular expressions are patterns used to match character combinations in strings. The RegExp object provides methods for creating and manipulating regular expressions, as well as for searching and replacing text.
Map: The Map object is used to store collections of key-value pairs. It is similar to an object, but with some differences in behavior and syntax. Maps can use any value as a key, including objects and functions, and they maintain the order of their entries.
Set: The Set object is used to store collections of unique values. It is similar to an array, but with the added constraint that each value can only appear once in the set. Sets can be used to efficiently check for the presence of a value in a collection, and they provide methods for performing set operations such as union, intersection, and difference.
Typed Arrays: Typed arrays are used to store collections of binary data in JavaScript. They are similar to arrays, but with the added constraint that they can only contain values of a specific type, such as integers or floats. Typed arrays provide efficient memory management and allow for high-performance computation on large datasets.
In JavaScript, variables can hold different data types, and their types can change during program execution. JavaScript uses dynamic typing, which means that variables are not bound to a particular data type and can hold different types of values at different times.
click here to go back