Operators in JavaScript are symbols used to perform operations on variables and values. They are essential for writing logic, calculations, and conditions
1. Arithmetic Operators
Used for mathematical calculations.
Operator
Description
Example
+
Addition
5 + 2 = 7
-
Subtraction
5 - 2 = 3
*
Multiplication
5 * 2 = 10
/
Division
10 / 2 = 5
%
Modulus (remainder)
5 % 2 = 1
**
Exponentiation
2 ** 3 = 8
Example:
let a = 10; let b = 3;console.log(a + b); // 13 console.log(a % b); // 1
In JavaScript, variables are declared using var, let, and const. These keywords define how variables behave in terms of scope and mutability. Modern JavaScript development strongly favors let and const because they provide better control and reduce unexpected bugs. If you’re building dynamic websites or applications, understanding variables is absolutely essential because they drive logic, interactivity, and data handling.
Why Variables Matter in Programming
Variables allow your program to process user input, perform calculations, and manage application state. Think of them as the memory of your application. Without variables, even simple tasks like storing a username or calculating a total price would be impossible.
Real-Life Analogy for Variables
Consider a backpack you carry every day. You can put books, gadgets, or food inside it. Some items you replace frequently, while others stay the same. That’s exactly how variables behave depending on how you declare them.
Types of Variable Declarations
Understanding var
The var The keyword is the oldest method for declaring variables in JavaScript. It is function-scoped, meaning it is accessible throughout the function in which it is defined. One unique characteristic of var is that it allows both redeclaration and reassignment, which can sometimes introduce bugs if not handled carefully.
var name = "John"; var name = "Doe"; // Allowed console.log(name); // Output: Doe
Because of these limitations, developers rarely use var in modern applications unless maintaining legacy code.
Understanding let
The let keyword was introduced in ES6 and is now widely used. It provides block-level scope, meaning the variable is only accessible within the block {} where it is defined. This makes code more predictable and easier to debug.
let age = 25; age = 30; // Allowed // let age = 35; ❌ Not allowed
Using let ensures that variables don’t accidentally leak outside their intended scope.
Understanding const
The const keyword is used to declare variables whose values should not change. Once assigned, a const variable cannot be reassigned. However, if the variable holds an object or array, its internal properties can still be modified.
const country = "India"; // country = "USA"; ❌ Error
This makes const ideal for values that should remain constant throughout the program.
Key Differences Between var, let, and const
Scope Explained
Keyword
Scope Type
var
Function/Global
let
Block
const
Block
Reassignment vs Redeclaration
Keyword
Reassign
Redeclare
var
Yes
Yes
let
Yes
No
const
No
No
Hoisting Behavior
JavaScript uses a mechanism called hoisting, where variable declarations are moved to the top of their scope during execution. However, var is initialized with undefined, while let and const remain uninitialized until execution reaches their declaration.
Rules for Naming Variables
Valid Naming Conventions
Start with a letter, _, or $
Can include numbers (not at the beginning)
Case-sensitive
Common Mistakes to Avoid
Using reserved keywords
Starting names with numbers
Using unclear variable names
Rules to define a variable name
✅ 1. Allowed Characters
Variable names can contain:
Letters (a–z, A–Z)
Digits (0–9)
Underscore (_)
Dollar sign ($)
✔ Example:
let name; let user1; let _count; let $price;
❌ 2. Cannot Start with a Number
A variable name must NOT start with a digit
❌ Wrong:
let 1name;
✔ Correct:
let name1;
❌ 3. No Spaces Allowed
Variable names cannot contain spaces
❌ Wrong:
let user name;
✔ Correct:
let userName;
❌ 4. Cannot Use Reserved Keywords
You cannot use JavaScript reserved words like:
let, var, const, if, else, function, etc.
❌ Wrong:
let var = 10;
✅ 5. Case Sensitive
JavaScript is case-sensitive
let name = "John"; let Name = "Doe"; // different variable
This Python Tuple program will check the initial value of all sub-tuples, if the initial value of two sub-tuple are the same, then it will merge both the tuple.
Input: [(3,6,7),(7,8,4),(7,3),(3,0,5)]
Output: [(3,6,7,0,5),(7,8,4,3)]
# take input list value that contains multiple tuples
l1 = [(3, 6, 7), (7, 8, 4), (7, 3), (3, 0, 5)]
# initiate a variable to store the required output
output = []
# initiate a loop with range of length of list l1.
for i in range(len(l1)):
# initiate nested loop
for j in range(i+1, len(l1)):
# check any two same tuple initial values are same
if l1[i][0] == l1[j][0]:
# if two tuple initial value are same, then combine both tuple.
# and store in output list.
output.append(tuple(list(l1[i]) + list(l1[j][1:])))
else:
continue
print(output)
This Python tuple program will add a tuple of values as row-wise elements in the tuple matrix.
Input: A = [[(‘sqa’, 4)], [(‘tools’, 8)]] B = (3,6)
Output: [[(‘sqa’, 4,3)], [(‘tools’, 8,6)]]
var_a = [[('sqa', 4)], [('tools', 8)]]
var_b = (3, 6)
print("Input A : ", var_a)
print("Input B : ", var_b)
output = []
# initiate a loop till length of var_a
for i in range(len(var_a)):
# get tuple value with the help of indexing of var_a and connvert into list
l1 = list(var_a[i][0])
# check if value of i is less than length of var_b
if i < len(var_b):
# append new value to the list
l1.append(var_b[i])
# now convert list into tuple and append to output list
output.append([tuple(l1)])
print(output)
Output:
Input A : [[('sqa', 4)], [('tools', 8)]]
Input B : (3, 6)
Output :
[[('sqa', 4, 3)], [('tools', 8, 6)]]