The JavaScript Operators
The JavaScript Operators
Rating:
In the world of programming languages, operators play a very important role. Without operators, the programming languages have little value. The operators perform many operations such as arithmetic operations, comparisons, assignment of values, etc. The JavaScript language consists of several operators for performing different operations.
The operators used in JavaScript are as follows:
The following are the arithmetic operators:
The following example will demonstrate the use of the addition operator:
function add()
{
var num1=parseInt(prompt("Enter a number"))
var num2=parseInt(prompt("Enter a number"))
var sum=num1+num2
alert(sum)
var diff=num1-num2
alert(diff)
var prod=num*num2
alert(prod)
var div=num1/num2
alert(div)
var mod=num%num2
alert(mod)
}
</script>
The addition operator can also be used to concatenate strings. The following example will demonstrate this:
function conc()
{
var str1=prompt("Enter your name")
var str2=prompt("Enter your age")
var str3="Name="+str1+" "+"Age="+str2
alert(str3)
}
</script>
- x++
- ++x
Both of the above statements mean x=x+1. But when these are assigned to a variable, the meaning is changed. Let us see this in the following example:
Suppose the value of x=10, then in the statement b=++x, the value of the variables x and b will be 11. It is so because b=++x contains two statements:
- b=x
- x=x+1
Since the statement executes from left to right and the ++ operator has more priority than the = operator, x will first be incremented to one, and then its value will be assigned to b.
In case of the statement b=x++, the value of b will be 10 and the value of x will be 11 because b=x++ contains two statements.
- b=x
- x=x+1
In this case, when the statement executes from left to right, it comes in contact with the variable x. The value of x is assigned to b and x is incremented to 1.
The following example will demonstrate the use of the increment operator:
function incropr()
{
var a=10
var b
++a
alert(a) //a=11
a++
alert(a) //a=12
b=++a
alert(a) //a=13
alert(b) //b=13
b=a++
alert(a) //a=14
alert(b) //b=13
}
</script>
- x--
- --x
Both of the above statements mean x=x-1. But when these are assigned to a variable, the meaning is changed. Let us see this in the following example:
Suppose the value of x=10, then in the statement b=--x, the value of the variables x and b will be 9. It is so because b=--x contains two statements:
- b=x
- x=x-1
Since the statement executes from left to right and the -- operator has more priority than the = operator, x will first be decremented by one, and then its value will be assigned to b.
In case of the statement b=x--, the value of b will be 10 and the value of x will be 9 because b=x-- contains two statements.
- b=x
- x=x-1
In this case, when the statement executes from left to right, it comes in contact with the variable x. The value of x is assigned to b and x is decremented by 1.
The following example will demonstrate the use of the decrement operator:
function decropr()
{
var a=10
var b
--a
alert(a) //a=9
a--
alert(a) //a=8
b=--a
alert(a) //a=7
alert(b) //b=7
b=a--
alert(a) //a=6
alert(b) //b=7
}
</script>
The following example will demonstrate the use of the negation operator:
function negate()
{
var a=10
var b=-a
alert(b)//b=-10
}
</script>
The following are the comparison operators:
function compare()
{
var a=10
var b=10
alert(a==b) //result is true
alert(a!=b) //result is false
alert(a>b) //result is false
alert(a>=b) //result is true
alert(a<b) //result is false
alert(a<=b) //result is true
}
</script>
The following are the assignment operators:
For example:
var b=a //The value of variable a is assigned to variable b.
The syntaxes of these operators are as follows:
| x+=y | x=x+y |
| x-=y | x=x-y |
| x*=y | x=x*y |
| x/=y | x=x/y |
| x%=y | x=x%y |
The following example will demonstrate the use of the assignment operators:
function assign()
{
var x=200
var y=10
x+=y
alert(x) //210
x=200
x-=y
alert(x) //190
x=200
x*=y
alert(x) //2000
x=200
x/=y
alert(x) //20
x=200
x%=y
alert(x) //0
}
</script>
The add by value operator can also be used for the concatenation of strings.
The following code will demonstrate this:
function conc()
{
var str="This is a string"
str+="This is another string"
alert(str)
}
</script>
The syntaxes of these operators are as follows:
| x>>=y | x=x>>y |
| x<<=y | x=x<<y |
| x>>>=y | x=x>>>y |
| x&= | x=x&y |
| x|=y | x=x|y |
| x^=y | x=x^y |
The following example will demonstrate the use of these operators:
function bitassign()
{
var x=128
var y=2
x>>=y
alert(x) //32
x=128
x<<=y
alert(x) //512
x=128
x>>>=y
alert(x) //32
x=128
x&=y
alert(x) //0
x=128
x|=y
alert(x) //102
x=128
x^=y
alert(x) //102
}
</script>
The following are the bitwise operators:
For example:
| 12 | 1100 |
| & | & |
| 10 | 1010 |
| = | = |
| 8 | 1000 |
For example:
| 12 | 1100 |
| | | | |
| 10 | 1010 |
| = | = |
| 14 | 1110 |
For example:
| 12 | 1100 |
| ^ | ^ |
| 10 | 1010 |
| = | = |
| 6 | 0110 |
For example:
| 7 | 0111 |
| -8 | 1000 |
For example:
| 12 | << | 2 | = | 48 |
| 1100 | << | 2 | = | 110000 |
| 12 | >> | 2 | = | 3 |
| 1100 | >> | 2 | = | 11 |
| 12 | >>> | 2 | = | 3 |
| 1100 | >> | 2 | = | 0011 |
The following code will demonstrate the use of the bitwise operators:
function bitopr()
{
var a=128
var b=2
var c=a & b
alert(c)
a=128
c=a | b
alert(c)
a=128
c=~a
alert(c)
a=128
c=a<<b
alert(c)
a=128
c=a>>b
alert(c)
a=128
c=a>>>2
alert(c)
}
</script>
There are three kinds of boolean operators as follows:
- &&
- ||
- !
In the case of the && operator, the result is true only if all the conditions are true. In the given conditions, if a condition is false, the result will be false.
Following table will show the result of evaluating two conditions:
| True | && | True | True |
| True | && | False | False |
| False | && | True | False |
| False | && | False | False |
In the case of the || operator, the result is false only if both the conditions are false. Otherwise, the result is true.
The following table will show the result of the or operator:
| True | || | True | True |
| True | || | False | True |
| False | || | True | True |
| False | || | False | False |
In the case of the not operator, if the condition is not false, it is true. If the condition is not true, it is false
The following table will show the result of the not operator:
| True | ! | False |
| False | ! | True |
The following code will demonstrate the use of the typeof operator:
function mytype()
{
var a=2
var b
alert(typeof a) //output is number
a="James"
alert(typeof a) // output is string
a=true
alert(typeof a) // output is boolean
a=document
alert(typeof a) // output is object
a=b
alert(typeof a) // output is undefined
}
</script>
The syntax of using the void operator is as follows:
For example:
Both variables a and b can now access the properties and functions of the Date object.
Instantiation can also be done with custom objects.
function car(name, model, year)
{
this.name=name
this.model=model
this.year=year
}
Here, m is the instance of the object car.
The syntax for using the delete operator is as follows:
The following example will demonstrate the use of the delete operator:
function mydel()
{
var arr=new Array(1,2,3,4,5)
alert(arr)
delete arr[0]
alert(arr)
}
</script>
The following table displays the operator precedence:
| () | Parentheses |
| [] | Braces |
| ! | Boolean NOT |
| ~ | Bitwise NOT |
| - | Negation |
| ++ | Increment |
| -- | Decrement |
| typeof | typeof |
| void | void |
| delete | delete |
| * | Multiplication |
| / | Division |
| % | Modulus |
| + | Addition |
| - | Subtraction |
| << | Bitwise left shift |
| >> | Bitwise right shift |
| >>> | Bitwise zero fill |
| < | Less than |
| <= | Less than or equal to |
| > | Greater than |
| >= | Greater than or equal to |
| == | Equal to (Comparison) |
| != | Not equal to |
| & | Bitwise AND |
| ^ | Bitwise XOR |
| | | Bitwise OR |
| && | Boolean AND |
| || | Boolean OR |
| ? | Conditional Operator |
| = | Equal to (Assignment) |
| *= | Multiply by value |
| /= | Divide by value |
| %= | Modulus by value |
| += | Add by value |
| - | Subtract by value |
| <<= | Left shift by value |
| >>= | Right shift by value |
| >>>= | Zero fill by value |
| &= | Bitwise AND by value |
| ^= | Bitwise XOR by value |
| |= | Bitwise OR by value |
| , | comma |
Rating:
Other articles
- What is the navigator object?
- What is a server object?
- The Window Object: Its Properties, Methods, and Events.
- Functions, Global Functions, and Custom Objects.
- What is the protocol property of the server object?
