What is an Operator in Programming / Scripting?
Operators are symbols respresenting certain operations. For example the operator '+' represents the sum or adding operation. The operations are performed on opearands such as values, variables etc.
Types of Operators in Javascript
Javascript uses similar types of operators as used in various other programming and scripting languages. Most commonly used javascript operator types are :-
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- String Operators
Javascript Arithmetic Operators
Artithmetic operators are used to perform arithmetic(a branch of mathematics) operations on variable names or values. Arithmetic Operations like addition, subtraction, multiplication etc.List of Artithmetic Operators
| + | Addition |
| - | Subtraction |
| / | Division |
| * | Multiplication |
| ++ | Increment (increase value) |
| -- | Decrement (decrease value) |
| % | Modulus (remainder when wholly divide a value/variable by another. see example below) . |
- y = 89-32;
- x = 2000 + expenses; (variable name)
- newsalary = oldsalary * 1.10; (newsalary and oldsalary are variable names).
- x = 98 % 4; (Modulus example. Result will be 2 as 4 divides 98 to a whole number 24 times i.e. 4*24 = 96 and what remains is 2) . Show remainder instead of value in decimal points
Javascript Assignment Operators
As the name suggests, a Javascript assignment operator is used to assign /Associate value to a varible or constant name.Assignment Operator '='
x = 5;age = 26;
This command tells the browser to assign value of '5' to the variable 'x' by using the assignment operator '='.
List of JavaScript Assignment Operators
| Examples | ||
| = | Assign a single value or expression | |
| += | Add the specified value to the current value of the variable. the result become value of the variable | x = 10; x += 5; The value of x now becomes 15 |
| -= | Subtract the specified value from the current value of the variable | x = 15; x -= 5; The value of x now becomes 10 |
| *= | Multiply the specified value with the current value of the variable | x = 10; x *= 5; The value of x now becomes 50 |
| /= | Divide the variable value with the specified value. | x = 50; x /= 25; The value of x now becomes 2 |
Alternate method of Assignment
x = 10;x = x +5; (This command has the same function as x += 5;)
It means that the new value of x becomes the older value of x plus 5.
Note that the x on left side means new 'x' while the one on right refers to the old value of the variable 'x'.
JavaScript String Operators - Concatenation
We studied the + and - operators as they are used in arithmetic operators. These operators are also used with Strings and text in Javascript. (See Javascript Variable Types for what Strings are). The '+' when used with strings is refered to as a 'Concatenation Operator'. Concatenation means to join.We declare two variables.
text1 = "hello ";
text2 = " welcome to our site";
Now we will use the + opertor to join these two variables.
greeting = text1 + "how are you?" + text2;
(declared a new variable called greeting and joined the two string variables text1 and text 2 and also some text.
RESULTThe value of variable greeting will now become.
"hello how are you? Welcome to our site"
Back to index of javascript
No comments:
Post a Comment