In JavaScript, variables are used to store data, which can be of different types. Here is an overview of the various data types and how to work with variables in JavaScript, along with examples.
1. Variables
In JavaScript, you can declare variables using var
, let
, and const
.
var
: The oldest way to declare variables. It has function scope and can be re-assigned.let
: Introduced in ES6, it has block scope and can be re-assigned.const
: Introduced in ES6, it has block scope and cannot be re-assigned.
Examples:
// Using var var x = 10; x = 20; // This is allowed // Using let let y = 30; y = 40; // This is allowed // Using const const z = 50; // z = 60; // This will cause an error
2. Data Types
JavaScript supports various data types, including:
- Primitive Data Types: String, Number, Boolean, Undefined, Null, Symbol, BigInt
- Reference Data Types: Objects, Arrays, Functions
Primitive Data Types:
- String: Represents textual data
let name = "John Doe"; console.log(typeof name); // "string"
Number: Represents both integer and floating-point numbers
let age = 25; let pi = 3.14; console.log(typeof age); // "number" console.log(typeof pi); // "number"
Boolean: Represents logical entities, true or false
let isStudent = true; console.log(typeof isStudent); // "boolean"
Undefined: A variable that has not been assigned a value
let notAssigned; console.log(typeof notAssigned); // "undefined"
Null: Represents the intentional absence of any object value
let emptyValue = null; console.log(typeof emptyValue); // "object" (this is a known quirk in JavaScript)
Symbol: A unique and immutable primitive value
let symbol1 = Symbol('description'); console.log(typeof symbol1); // "symbol"
BigInt: Represents whole numbers larger than 2^53 – 1
let bigNumber = BigInt(123456789012345678901234567890); console.log(typeof bigNumber); // "bigint"
Reference Data Types:
- Object: A collection of key-value pairs
let person = { name: "Alice", age: 30 }; console.log(typeof person); // "object"
Array: An ordered list of values
let numbers = [1, 2, 3, 4, 5]; console.log(typeof numbers); // "array"
Function: A block of code designed to perform a particular task
function greet() { console.log("Hello, world!"); } console.log(typeof greet); // "function"
Example Combining Variables and Data Types:
let name = "John"; // String let age = 25; // Number let isStudent = false; // Boolean let notAssigned; // Undefined let emptyValue = null; // Null let person = { // Object name: "Jane", age: 22 }; let numbers = [1, 2, 3]; // Array let greet = function() { // Function console.log("Hello!"); }; // Output the types console.log(typeof name); // "string" console.log(typeof age); // "number" console.log(typeof isStudent); // "boolean" console.log(typeof notAssigned); // "undefined" console.log(typeof emptyValue); // "object" console.log(typeof person); // "object" console.log(typeof numbers); // "arr" console.log(typeof greet); // "function"
In summary, understanding variables and data types in JavaScript is fundamental to writing effective code. Different variable declarations (var
, let
, const
) offer different scoping rules, and JavaScript supports a variety of data types, each with its own use cases and behaviors.