Quick Contact

    JavaScript Operators

    Operators are symbols or keywords that instruct the JavaScript engines to take certain actions. The addition (+) sign, for example, instructs the JavaScript engine will add two variables or variables, whereas the equal-to (==), greater-than (>), or less-than () symbols instruct the JavaScript engine to compare two variables or values, and so on.

    The sections that follow discuss the various JavaScript operators.

    JavaScript Arithmetic Operators

    The arithmetic operators is used to accomplish basic arithmetic operations including addition, subtraction, multiplication, and so on. The following is an exhaustive list of JavaScript’s arithmetic operators:

    Operator Description Example Result
    + Addition x + y Sum of x and y
    Subtraction x – y Difference of x and y.
    * Multiplication x * y Product of x and y.
    / Division x / y Quotient of x and y
    % Modulus x % y Remainder of x divided by y

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Arithmetic Operators< /title>
    < /head>
    < body>
    < script>
    var a = 20;
    var b = 5;
    document.write(a + b); 
    document.write("< br>");
    
    document.write(a - b); 
    document.write("< br>");
    
    document.write(a * b); 
    document.write("< br>");
    
    document.write(a / b);
    document.write("< br>");
    
    document.write(a % b); 
    < /script>
    < /body>
    < /html>
    

    Output

    25
    15
    100
    4
    0
    

    JavaScript Assignment Operators

    Variables are assigned values using assignment operators.

    Operator Description Example Is The Same As
    = Assign x = y x = y
    += Add and assign x += y x = x + y
    -= Subtract and assign x -= y x = x – y
    *= Multiply and assign x *= y x = x * y
    /= Divide and assign quotient x /= y x = x / y
    %= Divide and assign modulus x %= y x = x % y

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Assignment Operators< /title>
    < /head>
    < body>
    < script>
    var a;    // Declaring Variable
    
        a = 50;
    document.write(a + "< br>"); 
    
        a = 50;
        a += 20;
    document.write(a + "< br>"); 
    
        a = 50;
        a -= 30;
    document.write(a + "< br>"); 
    
        a = 50;
    a *= 3;
    document.write(a + "< br>"); 
    
        a = 50;
    a /= 10;
    document.write(a + "< br>"); 
    
        a = 50;
    a %= 5;
    document.write(a); 
    < /script>
    < /body>
    < /html>
    

    Output

    50
    70
    20
    150
    5
    0
    

    JavaScript String Operators

    There are two operations that may also be was using on strings.

    Operator Description Example Result
    + Concatenation str1 + str2 Concatenation of str1 and str2
    += Concatenation assignment str1 += str2 Appends the str2 to the str1

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript String Operators< /title>
    < /head>
    < body>
    < script>
    var str1 = "Hello";
    var str2 = " Friends!";
    
    document.write(str1 + str2 + "< br>"); 
    
        str1 += str2;
    document.write(str1); 
    < /script>
    < /body>
    < /html>
    

    Output

    Hello Friends!
    Hello Friends!
    

    JavaScript Incrementing and Decrementing Operators

    The increment/decrement expressions are utilized to increase or decrease the value for a variable.

    Operator Name Effect
    ++x Pre-increment Increments x by one, then returns x
    x++ Post-increment Returns x, then increments x by one
    –x Pre-decrement Decrements x by one, then returns x
    x– Post-decrement Returns x, then decrements x by one

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Incrementing and Decrementing Operators< /title>
    < /head>
    < body>
    < script>
    var x; // Declaring Variable
    
        a = 100;
    document.write(++a); 
    document.write("< p>" + a + "< /p>");   
    
        a = 100;
    document.write(a++); 
    document.write("< p>" + a + "< /p>");   
    
        a = 100;
    document.write(--a); 
    document.write("< p>" + a + "< /p>");   
    
        x = 100;
    document.write(a--); 
    document.write("< p>" + a + "< /p>");   
    < /script>
    < /body>
    < /html>
    

    Output

    101
    101
    100
    101
    99
    99
    99
    98
    

    JavaScript Logical Operators

    Conditional expressions is commonly combined using logical operators.

    Operator Name Example Result
    && And x && y True if both x and y are true
    || Or x || y True if either x or y is true
    ! Not !x True if x is not true

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Logical Operators< /title>
    < /head>
    < body>
    < script>
    var year = 2021;
    
        // Leap years are divisible by 400 or by 4 but not 100
    if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))){
    document.write(year + " is a leap year.");
        } else{
    document.write(year + " is not a leap year.");
        }
    < /script>
    < /body>
    < /html>
    

    Output

    2021 is not a leap year.

    JavaScript Comparison Operators

    The comparison operations are used to do a Boolean comparison between two variables.

    Operator Name Example Result
    == Equal x == y True if x is equal to y, and they are of the same type
    === Identical x === y True if x is equal to y, and they are of the same type
    != Not equal x != y True if x is not equal to y
    !== Not identical x !== y True if x is not equal to y, or they are not of the same type
    < Less than x < y True if x is less than y
    > Greater than x > y True if x is greater than y
    >= Greater than or equal to x >= y True if x is greater than or equal to y
    <= Less than or equal to x <= y True if x is less than or equal to y

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    
    < title>JavaScript Comparison Operators< /title>
    < /head>
    < body>
    < script>
    var a = 50;
    var b = 80;
    var c = "50";
    
    document.write(a == c);  
    document.write("< br>");
    
    document.write(a === c); 
    document.write("< br>");
    
    document.write(a != b);  
    document.write("< br>");
    
    document.write(a !== c); 
    document.write("< br>");
    
    document.write(a < b);   
    document.write("< br>");
    
    document.write(a > b);   
    document.write("< br>");
    
    document.write(a <= b);  
    document.write("< br>");
    
    document.write(a >= b);  
    < /script>
    < /body>
    < /html>
    

    Output

    true
    false
    true
    true
    true
    false
    true
    false
    

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.