JavaScript Operators

## JS Basic

JS HOME JS Introduction JS How To JS Output JS Statements JS Comments JS Variables JS Data Types JS Objects JS Functions JS Operators JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Breaks JS Errors JS Validation

## JS Advanced

JS Object JS Number JS String JS Date JS Array JS Boolean JS Math JS RegExp

## JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Events DOM Navigation DOM Nodes DOM Nodelist

## JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS PopupAlert JS Timing JS Cookies

## JS Libraries

JS Libraries JS jQuery JS Prototype

## JS Examples

JS Basic Examples JS Objects Examples JS DOM Examples JS HTML Examples JS Browser Examples JS Quiz JS Certificate JS Summary

## JS References

Javascript Objects HTML DOM Objects

# JavaScript Operators

« Previous
Next Chapter »

= is used to assign values.

+ is used to add 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;
z=2;
x=y+z;

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 of x | Result of y | Try it
--- | --- | --- | --- | --- | ---
+ | Addition | x=y+2 | 7 | 5 | Try it »
- | Subtraction | x=y-2 | 3 | 5 | Try it »
* | Multiplication | x=y*2 | 10 | 5 | Try it »
/ | Division | x=y/2 | 2.5 | 5 | Try it »
% | Modulus (division remainder) | x=y%2 | 1 | 5 | Try it »
++ | Increment | x=++y | 6 | 6 | Try it »
x=y++ | 5 | 6 | Try it »
-- | Decrement | x=--y | 4 | 4 | Try it »
x=y-- | 5 | 4 | 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=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 »

## The + Operator Used on Strings

The + operator can also be used to add string variables or text values together.

## 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 »

## 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!

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE