JavaScript fundamentals
JavaScript Fundamentals
JavaScript fundamentals lay the groundwork for understanding the language's core concepts, including variables and data types, operators and expressions, and control flow structures.
Variables and Data Types:
In JavaScript, variables are containers for storing data values. Variables can hold various types of data, including numbers, strings, booleans, arrays, objects, and more. Here's an example of declaring and initializing variables:
// Variable declaration and initialization
let name = "John";
let age = 30;
let isStudent = true;
let fruits = ["apple", "banana", "orange"];
let person = { firstName: "John", lastName: "Doe" };
Operators and Expressions:
Operators in JavaScript allow you to perform operations on variables and values. JavaScript supports various types of operators, including arithmetic, assignment, comparison, logical, and bitwise operators. Expressions are combinations of variables, values, and operators that evaluate to a single value. Here are some examples:
// Arithmetic operators
let sum = 5 + 3;
let difference = 10 - 5;
let product = 3 * 4;
let quotient = 20 / 5;
// Comparison operators
let isEqual = (5 === 5); // true
let isNotEqual = (10 !== 5); // true
let isGreater = (10 > 5); // true
let isLess = (3 < 10); // true
// Logical operators
let isLoggedIn = true;
let isAdmin = false;
let canAccessAdminPanel = isLoggedIn && isAdmin; // false
Control Flow: if Statements, Loops, and Switch Statements:
Control flow structures allow you to control the execution flow of your JavaScript code. if
statements are used to make decisions based on conditions. Loops, such as for
and while
, are used to execute a block of code repeatedly. Switch statements provide a way to execute different actions based on different conditions. Here are examples of each:
// if statement
let hour = 10;
if (hour < 12) {
console.log("Good morning!");
} else {
console.log("Good afternoon!");
}
// for loop
for (let i = 0; i < 5; i++) {
console.log("Iteration: " + i);
}
// while loop
let count = 0;
while (count < 3) {
console.log("Count: " + count);
count++;
}
// 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("Today is not Monday or Tuesday.");
}
Next Part : JavaScript Fundamentals
Comments
Post a Comment