JavaScript赋值运算符深入学习指南

2021年3月25日11:35:23 发表评论 355 次浏览

本文概述

``data=value``

``````// Lets take some variables
x=10
y=20

x=y // Here, x is equal to 20
y=x // Here, y is equal to 10``````

a + = b

a = a + b

a- = b

a = a-b

a * = b

a = a * b

a / = b

a = a / b

a％= b

a = a％b

a ** = b

a = a ** b

a << = b

a = a << b

a >> = b

a = a >> b

a＆= b

a = a＆b

a | = b

a = a | b

a ^ = b

a = a ^ b

JavaScript

``````<script>
let a = 2;
const b= 3;

// Expected output: 2
document.write(a);

document.write( '</br>' );

// Expected output: 4
document.write(a = b + 1);
</script>``````

``````2
4``````

JavaScript

``````<script>

let yoo=4;

document.write(foo=yoo-1); // 4-1

</script>``````

``3``

JavaScript

``````<script>

let yoo=5;

document.write(yoo=yoo*2); // 5*2

</script>``````

``10``

JavaScript

``````<script>

let yoo=10;
const moo=2;

document.write(yoo=yoo/moo); // 10/2

document.write( "</br>" );

document.write(yoo/=0); // Infinity

</script>``````

``````5
Infinity``````

JavaScript

``````<script>

let yoo=50;

document.write(yoo%=10); //zero

</script>``````

``0``

JavaScript

``````<script>

let yoo=2;
const moo=2;

// 2 raise to the power 2
document.write(yoo**moo);

</script>``````

``4``

JavaScript

``````<script>
var yoo=5; // 101

// 20(In Binary 10100)
document.write(yoo<<=2);
</script>``````

``20``

JavaScript

``````<script>
var yoo=5;

document.write(yoo>>=2); // 001

</script>``````

``1``

JavaScript

``````<script>
var yoo=5;

document.write(yoo&=2); // 000

</script>``````

``0``

JavaScript

``````<script>
var yoo=5;

// 7(In binary: 111)
document.write(yoo|=2);

</script>``````

``7``

JavaScript

``````<script>
var yoo=5;

document.write(yoo^=2); // 111

</script>``````

``7``