Blog Banner |
Programming Fundamentals of JavaScript
Variables:
Definition and Role:
Variables are fundamental components of any programming language, including
JavaScript. They act as containers to store data values, such as numbers,
strings, objects, and more, during the execution of a script. Variables play a
vital role in storing information that can be accessed and manipulated
throughout the program's lifecycle.
Keywords: var, let, and const:
JavaScript offers three keywords for declaring variables: `var`, `let`, and
`const`.
- `var`: Before the introduction of `let` and `const` in ECMAScript 6
(ES6), `var` was the primary keyword used to declare variables. However, `var`
has some scoping issues and is not recommended for modern JavaScript
development.
- `let`: Introduced in ES6, `let` is block-scoped, meaning it is
limited in scope to the block (enclosed by curly braces) where it is declared.
It allows the variable's value to be reassigned.
- `const`: Also introduced in ES6, `const` declares a constant variable
whose value cannot be changed after initialization. Like `let`, it is
block-scoped.
Examples of Variable Declarations and Assignments:
Let's explore some examples of variable declarations and assignments using the
different keywords:
// Using 'var': var age = 25; // Declaring a variable 'age' and assigning a numeric value 25 var name = "John"; // Declaring a variable 'name' and assigning a string value // Using 'let': let count = 10; // Declaring a variable 'count' and assigning a numeric value 10 count = count + 5; // Reassigning 'count' with the new value 15 // Using 'const': const PI = 3.14; // Declaring a constant variable 'PI' with the value 3.14
Operators:
Arithmetic Operators:
Arithmetic operators perform mathematical calculations on numeric values.
Common arithmetic operators include addition (+), subtraction (-),
multiplication (*), division (/), and the modulus operator (%).
var num1 = 10; var num2 = 5; var sum = num1 + num2; // Addition: 10 + 5 = 15 var difference = num1 - num2; // Subtraction: 10 - 5 = 5 var product = num1 * num2; // Multiplication: 10 * 5 = 50 var quotient = num1 / num2; // Division: 10 / 5 = 2 var remainder = num1 % num2; // Modulus: 10 % 5 = 0
Assignment Operators:
Assignment operators are used to assign values to variables. The most basic
assignment operator is `=`.
var x = 10; var y = 5; x += y; // Equivalent to x = x + y, so x becomes 15
Comparison Operators:
Comparison operators compare two values and return a Boolean value (true or
false) based on the comparison result.
var a = 10; var b = 5; var isEqual = a === b; // Equality check: false var isNotEqual = a !== b; // Inequality check: true var isGreater = a > b; // Greater than check: true var isLess = a < b; // Less than check: false var isGreaterOrEqual = a >= b; // Greater than or equal check: true var isLessOrEqual = a <= b; // Less than or equal check: false
Logical Operators:
Logical operators are used to combine and manipulate Boolean values. The
common logical operators are `&&` (logical AND), `||` (logical OR),
and `!` (logical NOT).
var p = true; var q = false; var result1 = p && q; // Logical AND: false var result2 = p || q; // Logical OR: true var result3 = !p; // Logical NOT: false
Control Flow Statements:
if-else Statements:
The `if` statement allows you to execute a block of code if a certain
condition is true. You can also use `else` to specify a block of code
to be executed when the condition is false.
var age = 18; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); }
switch Statement:
The `switch` statement allows you to perform different actions based on
different conditions.
var day = "Monday"; var message; switch (day) { case "Monday": message = "It's Monday. Back to work!"; break; case "Friday": message = "It's Friday! Weekend is coming."; break; default: message = "Enjoy your day!"; }
for Loop:
The `for` loop is used to execute a block of code multiple times with a
specified condition.
for (var i = 1; i <= 5; i++) { console.log("Count: " + i); }
while Loop:
The `while` loop continues to execute a block of code as long as the specified
condition is true.
var count = 1; while (count <= 5) { console.log("Count: " + count); count++; }
do-while Loop:
The `do-while` loop is similar to the `while` loop, but it always executes the
block of code at least once before checking the condition.
var count = 1; do { console.log("Count: " + count); count++; } while (count <= 5);
Conclusion:
Understanding variables, operators, and control flow statements forms the
bedrock of JavaScript programming. By mastering these fundamentals, you gain
the ability to store and manipulate data, perform various operations, and
control the flow of your JavaScript code, laying the groundwork for creating
dynamic and interactive web applications.