Skip to content

Latest commit

 

History

History
129 lines (109 loc) · 3.58 KB

File metadata and controls

129 lines (109 loc) · 3.58 KB

5. JavaScript Operators and Arithmetic operators

JavaScript operators are symbols or keywords used to perform operations on variables and values. They allow you to manipulate data and perform computations in your JavaScript code. JavaScript operators can be categorized into several types, including arithmetic, comparison, logical, assignment, and more. Here, I'll provide an overview of the most common types of JavaScript operators:

  1. Arithmetic Operators:

    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division)
    • % (Modulus, returns the remainder)
    • ++ (Increment by 1)
    • -- (Decrement by 1)
    let x = 10;
    let y = 5;
    let sum = x + y;
    let remainder = x % y;
    x++; // Increment 'x' by 1
    y--; // Decrement 'y' by 1
  2. Comparison Operators:

    • == (Equal to)
    • != (Not equal to)
    • === (Strict equal to)
    • !== (Strict not equal to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
    let a = 10;
    let b = 5;
    let isEqual = a === b;
    let isGreaterThan = a > b;
  3. Logical Operators:

    • && (Logical AND)
    • || (Logical OR)
    • ! (Logical NOT)
    let isTrue = true;
    let isFalse = false;
    let result = isTrue && isFalse; // Logical AND
    result = isTrue || isFalse; // Logical OR
    result = !isTrue; // Logical NOT
  4. Assignment Operators:

    • = (Assignment)
    • += (Add and assign)
    • -= (Subtract and assign)
    • *= (Multiply and assign)
    • /= (Divide and assign)
    • %= (Modulus and assign)
    let x = 10;
    x += 5; // Equivalent to x = x + 5
    x -= 3; // Equivalent to x = x - 3
  5. Ternary Operator (Conditional Operator):

    • condition ? expression_if_true : expression_if_false
    let age = 20;
    let canVote = age >= 18 ? "Yes" : "No";
  6. String Concatenation Operator:

    • + (Used to concatenate strings)
    let firstName = "John";
    let lastName = "Doe";
    let fullName = firstName + " " + lastName;
  7. Typeof Operator:

    • typeof (Used to check the data type of a value)
    let x = 10;
    let type = typeof x; // Returns "number"
  8. Comma Operator:

    • , (Used to separate expressions, and it returns the result of the last expression)
    let a = 1, b = 2, c = 3;
    let result = (a++, b++, c++); // result will be 3

These are some of the most commonly used JavaScript operators. Understanding how to use these operators is crucial for performing various operations in JavaScript, from basic arithmetic calculations to more complex logic and decision-making in your code.


Assignment and Task

Create a program that converts degree celsius to fahrenheit. The formula to convert celsius to fahrenheit is Formula: fahrenheit = celsius * 1.8 + 32. What you can do is store the value of celsius value in the celsius variable. Then use the formula fahrenheit = celsius * 1.8 + 32 to convert celsius to fahrenheit and display the result.

Solution:

let celcius = 88;
let fahrenheit = celcius * 1.8 + 32;
console.log(`${celcius} celcius equals to ${fahrenheit} fahrenheit`);

Output

88 celcius equals to 190.4 fahrenheit

p4n Quiz

Q. What is the output of the following code?

console.log(5**3);
  1. 555
  2. 15
  3. 125
  4. 8

Answer: 3