CSS 4,5,6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

FS23CO038

EXPERIMENT 4

Aim - Implement JavaScript program using if condition statement and


looping .

Theory –

If Condition Statements in JavaScript


In JavaScript, if condition statements are used to make decisions in code based on
conditions. The structure of if statements is as follows:

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

3. if...else if...else Statement


if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if both conditions are false
}
 Allows for multiple conditions, checked sequentially.
 The first true condition block executes, skipping the rest.

Looping Statements in JavaScript

Looping statements are used to repeat a block of code multiple times.

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.

4. for...of Loop (for Arrays or Iterable Objects)


for (let element of iterable) {
// Code to execute for each element in iterable
}
 Used to iterate over arrays or other iterable objects.
FS23CO038

Program:

Output:

Conclusion – In this experiment, I have learned about If statements and looping


statements.
FS23CO038

EXPERIMENT 5

Aim - Implement JavaScript to use array and associative array.

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 -

let numbers = [1, 2, 3, 4, 5];


let mixedArray = [42, "hello", true, { name: "Alex" }];

Accessing Array Elements -


You can access elements in an array by using their index:

console.log(numbers[0]); // Output: 1
FS23CO038

Associative Arrays (Objects in JavaScript):


JavaScript does not support associative arrays in the traditional sense (arrays with
named keys), but it allows objects to act similarly. In JavaScript, objects use key-
value pairs, where each key is a unique identifier.

Characteristics of Associative Arrays (Objects)


1. Key-value Pairs: Each element in an associative array is a key-value pair,
where the key is usually a string.
2. Non-ordered: Unlike arrays, the order of properties in an object is not
guaranteed.
3. Unique Keys: Each key is unique and is used to access its corresponding
value.

Declaring an Associative Array (Object):

let person = {
name: "John",
age: 30,
occupation: "Developer"
};

Accessing Values in an Object


You can access values by using dot notation or bracket notation with the key name:

console.log(person.name); // Output: "John"


console.log(person["age"]); // Output: 30
FS23CO038

Program:

Output:

Conclusion – In this experiment, I have learned about arrays and


associative arrays.
FS23CO038

EXPERIMENT 6

Aim - Write JavaScript program to implement string methods and functions

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.

let greeting = "Hello, World!";

String Methods in JavaScript:


JavaScript provides various built-in methods to work with strings, allowing you to
manipulate, search, and transform them.

Common String Methods-

length: Returns the total number of characters in a string.


toUpperCase(): Converts all characters in a string to uppercase.
toLowerCase(): Converts all characters in a string to lowercase.

slice(start, end): Extracts a portion of the string from the start index up to, but not
including, the end index.

indexOf(substring): Finds the first occurrence of a specified substring within the


string.
FS23CO038

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:

let result = add(5, 3); // result will be 8


FS23CO038

Program:

Output:

Conclusion – In this experiment, I have learned about strings and


different methods on string and also about function in JS.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy