Harshil Chovatiya - Day 3 and 4: Operators and Basic Input/Output, Control Flow in JavaScript

Harshil Chovatiya - Day 3 and 4: Operators and Basic Input/Output, Control Flow in JavaScript

Harshil Chovatiya - Day 3 and 4: Operators and Basic Input/Output, Control Flow in JavaScript

Day 3 : Operators and Basic Input/Output in JavaScript

Overview:

  • Operators allow you to perform operations on variables and values in JavaScript.
  • Input and output functions enable interaction with users.

In-Depth:

1. Arithmetic Operators:

Arithmetic operators (+, -, *, /, %) perform basic mathematical operations.

                            
        var num1 = 10;
        var num2 = 5;
        var sum = num1 + num2;
        var product = num1 * num2;
                            
                        

2. Comparison Operators:

Comparison operators (==, !=, >=) compare values and return a boolean result.

                            
        var age = 25;
        var isAdult = age >= 18;
                            
                        

3. Logical Operators:

Logical operators (&&, ||, !) are used to perform logical operations on boolean values.

                            
        var isStudent = true;
        var isWorking = false;
        var isEmployed = isStudent && isWorking;
                            
                        

4. Input and Output:

`prompt()` function allows you to take user input.

`alert()` function displays a pop-up message.

`console.log()` prints output to the browser console.

                            
        var userName = prompt("Enter your name:");
        alert("Hello, " + userName);
        var sum = "Hello  " + userName;
        console.log("Result:", sum);
                            
                        

Day 3 introduces operators for performing various operations on data and basic input/output functions. These are essential tools for creating interactive and dynamic JavaScript applications. As you progress, you'll learn to use these concepts to build more complex programs and user interfaces.

Day 4: Control Flow - Conditional Statements in JavaScript

Overview:

  • Conditional statements allow your program to make decisions based on conditions.
  • `if`, `else`, and `else if` statements help control the flow of your code.

In-Depth:

1. if Statements:

`if` statements are used to execute code when a specified condition is true.

                                
            var age = 20;
            if (age >= 18) {
            console.log("You are an adult.");
            }
                                
                            

2. else Statements:

`else` statements provide an alternative code block to execute when the condition in the `if` statement is false.

                                
            var age = 15;
            if (age >= 18) {
            console.log("You are an adult.");
            } else {
            console.log("You are a minor.");
            }
                                
                            

3. else if Statements:

`else if` allows you to specify additional conditions to check.

                                
            var score = 75;
            if (score >= 90) {
            console.log("A");
            } else if (score >= 80) {
            console.log("B");
            } else if (score >= 70) {
            console.log("C");
            } else {
            console.log("F");
            }
                                
                            

4. Nested if Statements:

You can nest `if` statements inside other `if`, `else if`, or `else` statements to create complex decision trees.

                                
            var age = 20;
            var isStudent = false;
            if (age >= 18) {
            if (isStudent) {
                console.log("You are an adult student.");
            } else {
                console.log("You are an adult.");
            }
            } else {
            console.log("You are a minor.");
            }
                                
                            

Day 4 focuses on conditional statements in JavaScript, which are crucial for controlling the flow of your code based on different conditions. Understanding how to use `if`, `else`, and `else if` statements will enable you to make your programs more dynamic and responsive to user input or changing data.

Comments