CSS 4,5,6
CSS 4,5,6
CSS 4,5,6
EXPERIMENT 4
Theory –
1. if Statement
if (condition) {
// Code to execute if condition is true
}
The condition is an expression that evaluates to true or false.
If condition is true, the code block inside {} will execute; otherwise, it will be
skipped.
2. if...else Statement
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
The else block executes only if condition is false.
FS23CO038
1. for Loop
for (initialization; condition; increment) {
// Code to execute each iteration
}
initialization: Executes once before the loop starts.
condition: Checked before each iteration; loop continues while condition is true.
increment: Executes after each loop iteration.
FS23CO038
2. while Loop
while (condition) {
// Code to execute each iteration
}
The loop continues as long as condition is true.
If condition is initially false, the loop never executes.
3. do...while Loop
do {
// Code to execute each iteration
} while (condition);
The loop executes the code block at least once, then checks the condition.
Program:
Output:
EXPERIMENT 5
Theory –
Arrays in JavaScript:
An array in JavaScript is a data structure used to store multiple values in a single
variable. Arrays are ordered and indexed, meaning each element has a numerical
index starting from 0.
Characteristics of Arrays -
1. Index-based: Elements are accessed using a numerical index, starting from 0.
2. Dynamic: Arrays can grow or shrink in size.
3. Heterogeneous: Arrays can contain different data types (e.g., numbers,
strings, objects).
Declaring an Array -
console.log(numbers[0]); // Output: 1
FS23CO038
let person = {
name: "John",
age: 30,
occupation: "Developer"
};
Program:
Output:
EXPERIMENT 6
Theory –
Strings in JavaScript:
In JavaScript, strings are sequences of characters used to represent text. They
can include letters, numbers, symbols, and spaces and are defined by enclosing
text within quotes (single, double, or backticks). Strings are immutable, meaning
they cannot be changed once created, although you can create new strings
based on operations on the original.
slice(start, end): Extracts a portion of the string from the start index up to, but not
including, the end index.
Functions in JavaScript:
A function is a reusable block of code that performs a specific task. Functions allow
you to encapsulate code, making it modular, easier to manage, and reusable. They
can take inputs, known as parameters, and may return an output.
Characteristics of Functions
1. Reusable: Functions can be called multiple times throughout a program
without needing to rewrite the code.
2. Encapsulation: Functions encapsulate functionality, allowing you to break
down complex problems into smaller, manageable pieces.
3. Parameters and Arguments: Functions can accept parameters, which are
placeholders for the values passed to the function when it is called. The actual
values are called arguments.
Function Declaration:
function functionName(parameters) {
// Code to be executed
}
Example:
function add(a, b) {
return a + b;
}
Calling a Function:
Once defined, you can call a function by using its name followed by parentheses,
passing any required arguments:
Program:
Output: