An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in the program to operate on data and variables.
JavaScript Operators
Arithmetic Operators
Arithmetic operators work on one or more numeric values yielding a single result.
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment by 1
– Decrement by 1
Examples:
Addition
var a = 3 + 9;
In the expression above, the addition operator adds 3 and 9 to yield 12 and the assignment operator assigns this value to the variable a.
Subtraction
var c = 25 - 8;
(c = 17)
Multiplication
var d = 50 * 3;
(d = 150)
Division
var e = 50 / 5;
(e = 10)
Modulus
The JavaScript Modulus Arithmetic operator returns the remainder left after division.
var a = 70 % 16;
(a is now equal to 6, since the remainder left
after dividing 70 by 16 is 6)
var b = 49 % 7;
(b equals 0)
Increment and decrement operators
These arithmetic operators are used for increasing or decreasing the value of a variable by 1. They are used very frequently in JavaScript for loop and in other routine operations.
var a = 3;
a++; (a = 4)
var b = 10;
b–; (b = 9)
The function of the increment operator (and the decrement operator) can also be achieved by adding the digit 1 to the variable as:
a = a + 1;
OR
b = b - 1;
So why use a++ instead of a = a + 1?
The reason is that the JavaScript engine does the calculation for increment and decrement operators much faster. The difference in speed is not perceptible for small calculations and loops, however, with thousands of calculations, you’ll notice that the processing with the increment or decrement operator is much faster.
String Operator
Just like numbers, variables can contain the string data type. Any text has to be stored as strings.
eg. var first_name = “Johnny”;
When assigning strings to variables, they have to be surrounded by quotes, single or double. If you forget this, JavaScript will throw an error. Note, even numerical digits can be stored as string data. In such cases, the number acts as a string data type and NOT as a numeric data type.
To concatenate two strings, you use the + string operator.
eg.
var first_name = "Johnny"; var last_name = "Griffin"; full_name = first_name + " " + last_name;
To introduce a space, we add a space character between the two variables.
Output:
full_name = “Johnny Griffin”;
Comparison and Logical operators
Comparison and Logical operators are used to test for true or false.
Given that x=5, the examples below explains the comparison operators:
Operator Description Example
== is equal to x==8 is false
!= is not equal x!=8 is true
> is greater than x>8 is false
< is less than x<8 is true
>= is greater than or equal to x>=8 is false
<= is less than or equal to x<=8 is true
Comparison operators can be used in conditional statements to compare values and take action depending on the result:
if (age<18) document.write(”Too young”);
Logical Operators
Logical operators are used in determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true
Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename=(condition)?value1:value2
Example
greeting=(visitor==”PRES”)?”Dear President “:”Dear “;
If the variable visitor has the value of “PRES”, then the variable greeting will be assigned the value “Dear President ” else it will be assigned “Dear”.
Post a Comment