ITSE 1411 Beginning Web
Operators JS Module 2 Discussion
Discussion
  1. Javascript has the following operators
    • Arithmetic operators in JavaScript are very similar to the ones you learned in mathematics with a few exceptions. In JavaScript, the only operator for multiplication is the '*' asterisk. Addition, subtraction, multiplication, division, and negation are the same. There are 3 additional arithmetic operators that are handy. When you want to increase a variable by 1, you may use the '++' increment operator. Placed before or after the variable, it will increment the variable by 1. The order this is done will be discussed later in precedence. The last arithmetic operator helps you find the remainder of a division. It is called the modulus operator and is written as '%'. You have used '%' in the past as a substitute for the word "percent". However, in JavaScript it is an operator that gives you the remainder. For example:

      Consider 5/2. The answer is 2 because 5 divided by 2 is 2.
      Consider 5%2. The answer is 1 because after you divide 5 by 2, you have a remainder of 1.
    • The assignment operator in JavaScript is written '='. In math, you used the symbol in an equation. There are NO equations in JavaScript. There are expressions (which are evaluated). The result of an evaluated expression can then be assigned to a variable using the '=' assignment operator. Assignments always assign right to left, the expression on the right side of the assignment operator and a single variable on the left side of the assignment operator.

      The assignment operator combined with an arithmetic operator is called a compound assignment. The compound assignment works for addition, subtraction, multiplication, division, and modules. The value to the right of the operation performs the task of the arithmetic operator with the variable to the left of the operator.
    • Comparison operators compares two operands and returns the Boolean value true or false depending on the result of the comparison. The operators are:
      Comparison Operators Table
      Equal ==
      Strict equal ===
      Not equal !=
      Strict not equal !==
      Greater than >
      Less than <
      Greater than or equal >=
      Less than or equal <=
    • The conditional operator is often used with the comparison operator. Two expressions are used with the conditional expression and one is executed depending on whether the conditional expression is true or false. The syntax is:

      (value1 comparison operator value2) ? expression executed when comparison is true : expression executed when comparison is false;

      Value1 and value2 can be either variables or values.
    • Logical operators are used to compare the results of two different comparisons.
      • The '&&' And operator is true when both comparisons are true; otherwise it is false.
      • The '||' Or operator is true as long either comparison result is true and false only when both comparison results are false.
      • The '!' Not operator reverses a comparison result. Using the Not operator, a true result becomes false and a false result becomes true.
    • String operators

      The only arithmetic operator that can be used with strings is the '+' operator.

      Numbers and strings can be used with the assignment operator. The only compound assignment operator that can be used with string is the '+='.

      Numbers or strings can use the comparison operators. When using strings, the letters are considered in alphabetical order, not the length of a string. All uppercase letters are less than lowercase letters. All numeric strings (numbers that are represented as text) are less than all letters.
    • Special operators are used for special purposes. A few that you will use soon are:
      Special Operators
      Property access . appends an object, method, or property to another object
      Array index [] Access an element of an array
      Parentheses () Used with a function to contain parameters. Used in an expression to determine the order of operations.
      Comma , Used to separate parameters in a parameter list,
    • Data type operator returns the data type of a variable. Syntax:
      typeof(variableName);
      Data Type Return Values
      Return ValueData type of identifier
      Number Integers and floating point numbers
      String Text strings
      Boolean True or false
      Object Objects, arrays, and null variables
      Function Functions
      Undefined Undefined variables
    • Operator Precedence is the order of operation that expressions are evaluated. The order you learned in math applies, there are just more operators in JavaScript than you had in math. When the operators are on the same level, then they are evaluated in the order that they appear left to right (or right to left depending on the operator).