JavaScript: Types of Variables
In JavaScript, variables are used to store and manipulate data. JavaScript is a dynamically typed language, meaning you don't need to specify the data type of a variable explicitly; the type is determined at runtime. There are several types of variables in JavaScript, and they can be broadly categorized into the following:
Primitive Data Types:
Number: Represents numeric values (integers or floating-point).
let num = 42; // integer
let floatNum = 3.14; // floating-point
String: Represents textual data.
let str = "Hello, World!";
Boolean: Represents true or false values.
let isTrue = true;
let isFalse = false;
Null: Represents the intentional absence of any object value.
let nullValue = null;
Undefined: Represents the uninitialized or undefined value.
let undefinedValue;
Symbol: Introduced in ECMAScript 6, symbols are unique and immutable primitive values, often used as unique identifiers.
let sym = Symbol("unique");
Object Data Type:
let person = {
name: "John",
age: 30,
isStudent: false
};
Special Data Types:
let bigNum = 123n;
Reference Data Types:
Array: Represents an ordered collection of values.
let numbers = [1, 2, 3, 4, 5];
Function: Functions are objects in JavaScript, and you can assign them to variables.
let add = function (a, b) {
return a + b;
};
Date: Represents a date and time.
let currentDate = new Date();
RegExp (Regular Expression): Represents a regular expression pattern.
let regex = /[a-zA-Z]+/;
These are the basic types of variables in JavaScript. Understanding these types is crucial for effective programming in the language. Keep in mind that JavaScript is a dynamically typed language, so a variable's type can change during runtime.
Thank you.