0% found this document useful (0 votes)
20 views

Variables and Conditional Statements CheatSheet

The document discusses variable declaration and initialization in JavaScript using var, let, and const. It also covers conditional statements including if, if/else, if/else if/else, switch, and the ternary operator. Variables can be declared with var, let, or const and initialized during or after declaration. Conditional statements like if/else execute different code blocks depending on whether the condition is true or false, while switch selects a code block based on the value of an expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Variables and Conditional Statements CheatSheet

The document discusses variable declaration and initialization in JavaScript using var, let, and const. It also covers conditional statements including if, if/else, if/else if/else, switch, and the ternary operator. Variables can be declared with var, let, or const and initialized during or after declaration. Conditional statements like if/else execute different code blocks depending on whether the condition is true or false, while switch selects a code block based on the value of an expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Variables

Declaration:

var variableName; // Old way (function-scoped/global scoped)


let variableName; // New way (block-scoped)
const constantName; // Constant (block-scoped)

Initialization:

variableName = value;
let variableName = value;
const constantName = value;

Example Codes:

// Declaration and initialization


var age;
age = 25;

// Declaration and initialization in one line


let name = "John";

// Constant variable
const PI = 3.14;

Conditional Statements

if Statement

if (condition) {
// Code block to be executed if condition is true
}

if-else Statement

if (condition) {
// Code block to be executed if condition is true
} else {
// Code block to be executed if condition is false
}

if-else if-else Statement

if (condition1) {
// Code block to be executed if condition1 is true
} else if (condition2) {
// Code block to be executed if condition2 is true
} else {
// Code block to be executed if none of the conditions are true
}

switch Statement

switch (expression) {
case value1:
// Code block to be executed if expression equals value1
break;
case value2:
// Code block to be executed if expression equals value2
break;
default:
// Code block to be executed if expression doesn't match any case
}

Ternary Operator (Conditional Operator)

(condition) ? expression1 : expression2;


// If condition is true, expression1 is evaluated, otherwise expression2 is evaluated

Example Codes:

// if statement
let temperature = 20;
if (temperature > 30) {
console.log("It's hot outside.");
}
//Nothing will be printed because the condition is false

// if-else statement
let hour = 14;
if (hour < 12) {
console.log("Good morning!");
} else {
console.log("Good afternoon!");
}
//Good afternoon will be printed because the condition inside the if statement is false

// if-else if-else statement


let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
//Grade C will be printed because the condition inside the second else if is true

// switch statement
let day = "Monday";
switch (day) {
case "Monday":
console.log("Today is Monday.");
break;
case "Tuesday":
console.log("Today is Tuesday.");
break;
default:
console.log("It's neither Monday nor Tuesday.");
}
//Today is Monday will be printed because the 1st case is true.

// Ternary operator
let isRaining = true;
let action = (isRaining) ? "Take an umbrella." : "Enjoy the weather.";
console.log(action
//Take an umbrella will be printed because the condition inside the isRaining is true

Variables

Declaration:

Variables in JavaScript can be declared using the var, let, or const keywords. var is the old way of
declaring variables, which is function-scoped. let and const are the new ways, introduced in ES6, which
are block-scoped.

Initialization:

Variables can be initialized at the time of declaration or later in the code by assigning them a value
using the assignment operator =.

Example Codes:

// Declaration and initialization


var age;
age = 25;

// Declaration and initialization in one line


let name = "John";

// Constant variable
const PI = 3.14

Conditional Statements

if Statement
The if statement allows you to execute a block of code if a specified condition is true.

if-else Statement

The if-else statement allows you to execute one block of code if the condition is true and another
block of code if the condition is false.

if-else if-else Statement

The if-else if-else statement allows you to check multiple conditions and execute a block of code
based on the first condition that is true.

switch Statement

The switch statement allows you to select one of many code blocks to be executed based on the value
of an expression.

Ternary Operator (Conditional Operator)

The ternary operator ? : is a concise way to write an if-else statement in a single line.

You might also like