JavaScript Decision-Making Unraveled: A Deep Dive into Variables, Operators, and Control Flow
JavaScript Basic
JavaScript is versatile programming language that commanly used in web-development . But also found in other enviroment like node apache …etc .
Lightweight & Interpreted
Javascript is design to be fast and efficient , meaning it does’nt required a heavy system resource .
It’s is an Interpreted language , meaning it code run line by line without need seprate compilation setup .
In modern enviroments , it can also be compile source code to machine understandable code at a run time .
Understanding Variables in JavaScript
In real world we keep different thing in store in organise way form and some thing here and there means no any fix place where that store . In computer world same thing happening with data (video data, audio data, pdf etc) for this all need to before we store somewhere . If i play a youtube video that video as a chunk part store in my device then that play, when i play music on spotify then that is also store music data in memory, same thing with offline video and audio or other somewhere must be store in device then that play .
Defination :
A Variable in Javascript is a named space memory location that holds value . It act like a container for data that can be used and modified throughout a program. Variable are essential for making decision and create dynamic application .
var , let and const
var : It’s variable decalre globally-scoped and redeclaration . It’s is not blocked scope But fucntional scoped
var x = 10;
var x = 15;
// Block scoped
{
var x = 16;
}
console.log(x);
// Output: 16
// Functional scoped
function printNum(){
var y = 12;
console.log(y);
}
console.log(y);
// Output: y is not defined
function printer(){
console.log(`I have Iphone mode ${x}`)
};
printer();
// Output: I have Iphone mode 16
so we with same name variable name redeclare .
let : It’s variable decalre that not globally-scoped and not redeclaration . It ‘s blocked scoped and fucntional scoped .
let y = 40;
// Block scoped
{
let y = 50;
}
console.log(y);
// Output: 40
// Functional scoped
function printNum(){
var z = 12;
console.log(y);
}
console.log(z);
// Output: z is not defined
function printer(){
console.log(`I have ${y}k rupees .`)
};
printer();
// Output: `I have 40k rupees .
const : It’s variable decalre that not globally-scoped and not redeclaration . It ‘s blocked scoped and fucntional scoped .
Data Type :
Before know dive in Data Type first we need to understamd why need data type , is no any role data type my real life .
Let’s my younger brother which name is “Shantanu“ and his age is 10, his height is 119.3 cm, and my brother is too naughty he is always switch on/off room switch , his date-of-birth is 12-02-2015.
So with above example we easily see we use every where a differnt type of data, like name, height (flaot value), date, on & off , age
- Data type is just a classification of data on how to store and use it.
Operator & Expressions
Expression: It is valid unit of code that resolve to a value .
let x = 10 + 5; // 10 + 5 is an expression that evaluates to 15
let y = x * 2; // x * 2 is an expression that evalute to 30
console.log(y > 20); // y > 20 is a boolean expression that evalute to true
Two type of expression :
Those that have assigning Values e.g -
x = 7
In this example expression self assign to value 7 to the variable
Those that completly evalute e.g -
3 + 4
3 + 4
is a expression that uses a+
operator to add3
&4
Control Flow in JavaScript: If , Else, and Switch Case
Control flow statements determine the order in which code executes. By default, JavaScript runs code line by line (top to bottom), but control flow statements can change this behavior by using conditions and loops .
Conditional Statements (Decision Making)
These statements execute code on;y if condition is true or met .
if
Statement :
Execute only if condition true .
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
if…else
Statement :
Execute one block if true
, another id false
.
let age = 16;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You are too young to vote.");
}
if...else if...else
Statement :
Used for multiple conditions .
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
Switch
Statement:
Imagine you walk into a juice shop, and you see a menu like this:
1️⃣ Apple Juice
2️⃣ Mango Juice
3️⃣ Orange Juice
4️⃣ Pineapple Juice
You tell the shopkeeper a number, and they give you the correct juice. This is exactly how the switch
statement works in JavaScript!
A switch
statement is used to check a value and execute different code depending on that value. It is useful when there are many conditions to check.
let choice = 2; // User's choice (change this number to test different cases)
switch (choice) {
case 1:
console.log("You ordered Apple Juice! 🍏");
break;
case 2:
console.log("You ordered Mango Juice! 🥭");
break;
case 3:
console.log("You ordered Orange Juice! 🍊");
break;
case 4:
console.log("You ordered Pineapple Juice! 🍍");
break;
default:
console.log("Invalid choice! Please select a number between 1 and 4.");
}
How Does This Work? 🧐
1️⃣ The program takes a number (1-4) as input.
2️⃣ The switch
statement checks the value of choice
.
3️⃣ If choice
is 1, it prints "You ordered Apple Juice! 🍏"
and stops (because of break
).
4️⃣ If choice
is 2, it prints "You ordered Mango Juice! 🥭"
, and so on.
5️⃣ If the number does not match any case, the default
block runs, showing an error message.
Comparison Table of var
, let
, and const
Feature | var | let | const |
Scope | Function or global scope | Block scope (e.g., inside loops, conditionals) | Block scope (like let ) |
Hoisting | Hoisted to the top of the function, but initialized with undefined | Hoisted to the top of the block, but not initialized | Hoisted to the top of the block, but not initialized |
Reassignable | Yes | Yes | No |
Redeclaration | Yes (within the same scope) | No | No |
Temporal Dead Zone (TDZ) | Yes (TDZ from the start of the block until it's declared) | Yes (TDZ from the start of the block until it's declared) | Yes (TDZ from the start of the block until it's declared) |