JavaScript operators

Operator = is used for assignment.

Operator + is used to add value.

Operator = is used to assign values ​​to JavaScript variables.

The arithmetic operator + is used to add values.


Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<p>点击按钮计算 x 的值.</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
y=5;
z=2;
x=y+z;
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>

Run the program and try it


JavaScript Arithmetic Operators

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

Given y=5, the following table explains these arithmetic operators:

##%Find the remainder (keep the integer)x=y%2x=1++accumulationx=++yx=6--Decreasingx=--yx=4
OperatorDescriptionExampleResult
+Addx=y+2x=7
-minusx=y-2x=3
*Multiplyx=y*2x=10
/divide x=y/2x=2.5

JavaScript assignment operator

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

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


Operation SymbolExample is equivalent toResult##=+=-=*=/=%=



+ operator for strings

+ operator is used to convert a text value or String variables are added together (concatenated).

To connect two or more string variables, please use the + operator.

Example

If you need to connect two or more string variables, please use the + operator:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<p>点击按钮创建及增加字符串变量。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
document.getElementById("demo").innerHTML=txt3;
}
</script>
</body>
</html>

Run the program and try it


To add spaces between two strings, you need to insert spaces into a string:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<p>点击按钮创建及增加字符串变量。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
txt1="What a very ";
txt2="nice day";
txt3=txt1+txt2;
document.getElementById("demo").innerHTML=txt3;
}
</script>
</body>
</html>

Run the program and try it


Or insert spaces into the expression:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<p>点击按钮创建及增加字符串变量。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
txt1="What a very";
txt2="nice day";
txt3=txt1+" "+txt2;
document.getElementById("demo").innerHTML=txt3;
}
</script>
</body>
</html>

Run the program and try it


Add strings and numbers

Add two numbers and return the sum of the added numbers. If a number is added to a string, a string is returned. The following example:

Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<p>点击按钮创建及增加字符串变量。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x=5+5;
var y="5"+5;
var z="Hello"+5;
var demoP=document.getElementById("demo");
demoP.innerHTML=x + "<br>" + y + "<br>" + z;
}
</script>
</body>
</html>

Rule: If you add a number to a string, the result will be a string!

Run the program and try it



Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>点击按钮计算 x 的值.</p> <button onclick="myFunction()">点击这里</button> <p id="demo"></p> <script> function myFunction() { y=5; z=2; x=y+z; document.getElementById("demo").innerHTML=x; } </script> </body> </html>
submitReset Code
x=y x=5
x+=yx=x+yx=15
x-=yx=x-yx=5
x*=yx=x*yx=50
x/=yx=x/yx=2
x%=yx=x%yx=0