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 quotes
let 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';
// valid
let $age =
25;
// valid
let _isActive =
true;
// valid
let 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 type
let score =
95;
// Number data type
let isPassed =
true;
// Boolean data type
let grade;
// Undefined variable
const maxScore =
100;
// Constant value, cannot be reassigned
console.
log(username);
// Outputs: Alice
console.
log(score);
// Outputs: 95
console.
log(isPassed);
// Outputs: true
console.
log(grade);
// Outputs: undefined
Modifying Arrays and Objects:
const fruits = [
'apple',
'banana',
'cherry'];
fruits.
push(
'orange');
// You can modify elements of a const array
console.
log(fruits);
// Outputs: ['apple', 'banana', 'cherry', 'orange']
const person = {
name:
'John',
age:
25 };
person.
age =
30;
// You can modify properties of a const object
console.
log(person);
// Outputs: { name: 'John', age: 30 }
Key Points to Remember:
let
andconst
are preferred overvar
due to block-level scoping.const
is 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