In JavaScript, understanding data types, variables, and constants is crucial for writing efficient and functional code.
1. Data Types in JavaScript
JavaScript is a dynamically typed language, meaning you don’t need to
specify data types when declaring variables; the type is automatically
assigned. The main data types in JavaScript are:
Primitive Data Types:
These are the most basic data types and are immutable (cannot be changed).
·
String: Used to represent text.
let name = "John"; // Double quoteslet message = 'Hello'; // Single quotes
·
Number: Represents both
integers and floating-point numbers.
let age = 25;let price = 99.99;
·
Boolean: Represents true or
false.
let isActive = true;let isComplete = false;
·
Undefined: A variable that has
been declared but has not yet been assigned a value.
let x;console.log(x); // undefined
·
Null: Represents the
intentional absence of any object value.
let y = null;
·
Symbol: A unique and immutable
data type (introduced in ES6).
let sym = Symbol('description');
·
BigInt: Allows you to work with
integers larger than 2^53 - 1.
let bigNumber = 123456789012345678901234567890n;
Composite (Non-Primitive) Data Types:
·
Object: A collection of
properties (key-value pairs).
let person = { name: 'John', age: 30, isStudent: true};
·
Array: Used to store multiple
values in a single variable.
let colors = ['red', 'green', 'blue'];
2. Variables in JavaScript
Variables are containers that store data values. In modern JavaScript, you
can declare variables using var,
let, and const.
Declaring Variables
·
let:
This declares a block-scoped variable, which means it can be reassigned but
only exists within the block (e.g., inside a function or loop) it is declared.
let city = 'New York';city = 'Los Angeles'; // Valid reassignment
·
const:
This declares a constant block-scoped variable, meaning the value assigned to
it cannot be reassigned.
const country = 'India';// country = 'USA'; // This will throw an error, as you can't reassign a const variable
·
var:
This is the old way of declaring variables, and it has function-level scope
(not block-level). var
should generally be avoided due to its sometimes unpredictable behavior.
var age = 30;
Variable Naming Rules
·
Names must start with a letter, underscore _, or dollar sign $.
·
After the first character, numbers are allowed
(e.g., name1 is valid).
·
JavaScript variable names are case-sensitive
(Name and name are different).
let firstName = 'John'; // validlet $age = 25; // validlet _isActive = true; // validlet 1name = 'Alice'; // invalid (cannot start with a number)
3. Constants in JavaScript
A constant is a variable whose value cannot be changed once
it has been assigned. You declare constants using the const keyword. Constants are
block-scoped like variables declared with let.
Declaring Constants
·
Once assigned, a constant's value cannot be
reassigned.
·
It must be initialized at the time of
declaration.
const pi = 3.14159; // Constant variable
Const with Objects and Arrays
Although you can't reassign a constant, if the constant is an object
or an array, you can still modify the object's properties or
array's elements. The reference remains the same, but the contents can be
changed.
·
With Objects:
const car = { brand: 'Tesla', model: 'Model S' };car.model = 'Model X'; // Allowed (modifying property)
·
With Arrays:
const colors = ['red', 'green', 'blue'];colors.push('yellow'); // Allowed (modifying array)
However, reassigning the entire object or array would not be allowed:
const colors = ['red', 'green'];colors = ['blue', 'yellow']; // Error
Examples
Using Different Data Types, Variables, and Constants:
let username = 'Alice'; // String data typelet score = 95; // Number data typelet isPassed = true; // Boolean data typelet grade; // Undefined variable const maxScore = 100; // Constant value, cannot be reassigned console.log(username); // Outputs: Aliceconsole.log(score); // Outputs: 95console.log(isPassed); // Outputs: trueconsole.log(grade); // Outputs: undefined
Modifying Arrays and Objects:
const fruits = ['apple', 'banana', 'cherry'];fruits.push('orange'); // You can modify elements of a const arrayconsole.log(fruits); // Outputs: ['apple', 'banana', 'cherry', 'orange'] const person = { name: 'John', age: 25 };person.age = 30; // You can modify properties of a const objectconsole.log(person); // Outputs: { name: 'John', age: 30 }
Key Points to Remember:
letandconstare preferred overvardue to block-level scoping.constis used when you don’t want the variable to be reassigned, but it doesn’t mean the value inside an object or array is immutable.- JavaScript
is case-sensitive, so be careful with variable names.
- Different
data types can be assigned to variables without the need to specify them.
0 Comments