Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Js Lab

The document discusses JavaScript operators, conditional statements, loops, and functions. It provides examples of arithmetic, assignment, comparison, logical operators and string concatenation. Examples are also given for if/else statements, ternary operator, switch statement, for, while, do-while loops. Function definition and calling functions are demonstrated.

Uploaded by

Samuel Ketema
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Js Lab

The document discusses JavaScript operators, conditional statements, loops, and functions. It provides examples of arithmetic, assignment, comparison, logical operators and string concatenation. Examples are also given for if/else statements, ternary operator, switch statement, for, while, do-while loops. Function definition and calling functions are demonstrated.

Uploaded by

Samuel Ketema
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JS-LAB

• // Example: Operators in JavaScript


• // Arithmetic Operators
• let x = 10, y = 5;
• console.log("Arithmetic Operators:");
• console.log("x + y =", x + y); // Addition
• console.log("x - y =", x - y); // Subtraction
• console.log("x * y =", x * y); // Multiplication
• console.log("x / y =", x / y); // Division
• console.log("x % y =", x % y); // Modulus (Remainder)
• // Assignment Operators
• let a = 10;
• console.log("\nAssignment Operators:");
• a += 5; // Equivalent to: a = a + 5
• console.log("a after += 5:", a);
• a -= 3; // Equivalent to: a = a - 3
• console.log("a after -= 3:", a);
• a *= 2; // Equivalent to: a = a * 2
• console.log("a after *= 2:", a);
• a /= 4; // Equivalent to: a = a / 4
• console.log("a after /= 4:", a);
• a %= 3; // Equivalent to: a = a % 3
• console.log("a after %= 3:", a);
• // Comparison Operators
• let p = 10;
• let q = 5;
• console.log("\nComparison Operators:");
• console.log("p > q:", p > q);
• console.log("p < q:", p < q);
• console.log("p >= q:", p >= q);
• console.log("p <= q:", p <= q);
• console.log("p === q:", p === q); // Strict equality
• console.log("p !== q:", p !== q); // Strict inequality
• // Logical Operators
• let isTrue = true;
• let isFalse = false;
• console.log("\nLogical Operators:");
• console.log("isTrue && isFalse:", isTrue && isFalse); // Logical AND
• console.log("isTrue || isFalse:", isTrue || isFalse); // Logical OR
• console.log("!isTrue:", !isTrue); // Logical NOT
• // String Concatenation Operator
• let firstName = "John";
• let lastName = "Doe";
• console.log("\nString Concatenation Operator:");
• // Sample JavaScript code with conditional statements

• // Example 1: Basic if-else statement


• let temperature = 25;

• if (temperature > 30) {


• console.log("It's a hot day!");
• } else if (temperature >= 20 && temperature <= 30) {
• console.log("It's a nice day.");
• } else {
• console.log("It's cold today.");
}

• // Example 2: Ternary operator


• let isRaining = true;
• let rainingMessage = isRaining ? "Remember to take an umbrella!" : "No need for an
umbrella.";
• console.log(rainingMessage);
• // Example 3: Switch statement
• let dayOfWeek = "Monday";
• let schedule;

• switch (dayOfWeek) {
• case "Monday":
• schedule = "Work";
• break;
• case "Tuesday":
• case "Wednesday":
• case "Thursday":
• case "Friday":
• schedule = "Work";
• break;
• case "Saturday":
• case "Sunday":
• schedule = "Weekend";
• break;
• default:
• schedule = "Unknown";
• }
• console.log("Today's schedule:", schedule);
• // Example: Loops in JavaScript
• console.log("For loop:");
• for (let i = 0; i < 5; i++) {
• console.log("Iteration", i);
• }
• // Example: for...in loop in JavaScript
• // Define an object with some properties
• const person = {
• name: "Alice",
• age: 30,
• city: "New York"
• };
• // Iterate over the properties of the object using for...in loop
• console.log("Properties of the person object:");
• for (let key in person) {
• console.log(key + ": " + person[key]);
• }
• // While loop
• console.log("\nWhile loop:");
• let j = 0;
• while (j < 5) {
• console.log("Iteration", j);
• j++;
• }

• // Do-while loop
• console.log("\nDo-while loop:");
• let k = 0;
• do {
• console.log("Iteration", k);
• k++;
• } while (k < 5);
• // Example: Functions in JavaScript

• // Define a function called greet


• function greet(name) {
• console.log("Hello, " + name + "!");
• }

• // Call the greet function


• greet("Alice");

• // Define a function called add that returns the sum of two numbers
• function add(a, b) {
• return a + b;
• }

• // Call the add function and store the result in a variable


• let result = add(3, 5);

• // Display the result


• console.log("The sum is:", result);

You might also like