Topics we'll cover
- Variables and Data Types
- Operators and Expressions
- Conditional Statements
- Loops
1. Variables and Data Types
In JavaScript, variables are used to store and manage data. Think of them as containers for holding information. There are three ways to declare variables: var
, let
, and const
. Let's briefly explain each and cover some common data types:
-
var
: Historically used to declare variables but is now less commonly used. Variables declared with var have function-level scope. -
let
: Introduced in ES6 (ECMAScript 2015), it allows you to declare variables with block-level scope. This is the preferred way to declare variables in modern JavaScript. -
const
: Also introduced in ES6, const is used to declare constants. Once a value is assigned to a const, it cannot be changed.
Common Data Types
JavaScript has several data types, but here are some of the most common ones:
-
Number: Used for numeric values. Example:
let age = 25;
-
String: Used for text. Example:
let name = "John";
-
Boolean: Represents true or false values. Example:
let isStudent = true;
2. Operators and Expressions
Operators are symbols that perform operations on variables and values. Here are some of the basic operators:
Arithmetic Operators: Used for mathematical calculations. Examples:
String Concatenation: The + operator can also be used to concatenate strings.
3. Conditional Statements
Conditional statements are used to make decisions in your code. The most common one is the if
statement. Here's how it works:
In this example, if the condition (age >= 18)
is true, the code within the first block will execute; otherwise, the code in the else
block will execute.
You can also use else if
for multiple conditions:
4. Loops
Loops allow you to repeat actions in your code. There are two common types of loops in JavaScript:
for Loop: Executes a block of code a specified number of times. Here in this code is 5 times.
output:
while Loop
: Repeats a block of code while a condition is true.
output:
These loops are handy for tasks like iterating through arrays, processing lists of data, and performing repetitive operations.
Top comments (0)