X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Operators


= is used to assign values, + is used to add values, ++ is used to increment values.


The assignment operator = is used to assign values to JavaScript variables.

The arithmetic operator + is used to add values together.

Example

Assign values to variables and add them together:

y = 5;           // assign the value 5 to y
z = 2;           // assign the value 2 to z
x = y + z;       // assign the value 7 (y + z) to x 

The result of x will be:

7

Try it Yourself »


JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.

Given that y = 5, the table below explains the arithmetic operators:

Operator Description Example Result Result Try it
+ Addition x = y + 2 y = 5 x = 7 Try it »
- Subtraction x = y - 2 y = 5 x = 3 Try it »
* Multiplication x = y * 2 y = 5 x = 10 Try it »
/ Division x = y / 2 y = 5 x = 2.5 Try it »
% Modulus (division remainder) x = y % 2 y = 5 x = 1 Try it »
++ Increment x = ++y y = 6 x = 6 Try it »
x = y++ y = 6 x = 5 Try it »
-- Decrement x = --y y = 4 x = 4 Try it »
x = y-- y = 4 x = 5 Try it »


JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

Given that x = 10 and y = 5, the table below explains the assignment operators:

Operator Example Same As Result Try it
= x = y x = y x = 5 Try it »
+= x += y x = x + y x = 15 Try it »
-= x -= y x = x - y x = 5 Try it »
*= x *= y x = x * y x = 50 Try it »
/= x /= y x = x / y x = 2 Try it »
%= x %= y x = x % y x = 0 Try it »


JavaScript String Operators

The + operator can also be used to concatenate (add) strings.

Note When used on strings, the + operator is called the concatenation operator.

Example

To add two or more string variables together, use the + operator.

txt1 = "What a very";
txt2 = "nice day";
txt3 = txt1 + txt2;

The result of txt3 will be:

What a verynice day

Try it Yourself »

To add a space between the two strings, insert a space into one of the strings:

Example

txt1 = "What a very ";
txt2 = "nice day";
txt3 = txt1 + txt2;

The result of txt3 will be:

What a very nice day

Try it Yourself »

or insert a space into the expression:

Example

txt1 = "What a very";
txt2 = "nice day";
txt3 = txt1 + " " + txt2;

The result of txt3 will be:

What a very nice day

Try it Yourself »

The += operator can also be used to concatenate strings:

Example

txt1 = "What a very ";
txt1 += "nice day";

The result of txt1 will be:

What a very nice day

Try it Yourself »


Adding Strings and Numbers

Adding two numbers, will return the sum, but adding a number and a string will return a string:

Example

x = 5 + 5;
y = "5" + 5;
z= "Hello" + 5;

The result of x, y, and z will be:

10
55
Hello5

Try it Yourself »

The rule is: If you add a number and a string, the result will be a string!


JavaScript Bitwise Operators

Bit operators work on 32 bits numbers.

Any numeric operand in the operation is converted into a 32 bit number.

The result is converted back to a JavaScript number.

Example

x = 5 & 1;

The result in x:

1

Try it Yourself »

Operator Description Example Same as Result Decimal
& AND x = 5 & 1 0101 & 0001 0001 1
| OR x = 5 | 1 0101 | 0001 0101 5
~ NOT x = ~ 5  ~0101 1010 10
^ XOR x = 5 ^ 1 0101 ^ 0001 0100 4
<< Left shift x = 5 << 1 0101 << 1 1010 10
>> Right shift x = 5 >> 1 0101 >> 1 0010 2

Note The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.

Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.

~00000000000000000000000000000101 will return 11111111111111111111111111111010


The typeof operator

The typeof operator returns the type of a variable (or an expression):

Example

var x = 5;
var y = "John";
typeof x                      // Returns number
typeof y                      // Returns string
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object
typeof {name:'john', age:34}  // Returns object

Try it yourself »


The delete Operator

The delete operator can be used to delete properties from objects:

Example

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age;

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

The delete operator should not be used on predefined JavaScript object properties. It can crash your application.


The Unary + Operator

The unary + operator can be used to convert a variable to a number:

Example

var y = "5";      // y is a string
var x = + y;      // x is a number

Try it yourself »

If the variable cannot be converted, it will still become a number, but with the value NaN (Not a number):

Example

var y = "John";   // y is a string
var x = + y;      // x is a number (NaN)

Try it yourself »


JavaScript Comparison and Logical Operators

Comparison and logical operators are described in the JS Comparisons chapter.




Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook