การกำหนด JavaScript


สารบัญ

    แสดงสารบัญ


ตัวดำเนินการกำหนด JavaScript

ตัวดำเนินการมอบหมายจะกำหนดค่าให้กับตัวแปร JavaScript

Operator Example Same As
= x = y x = y
+= 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
**= x **= y x = x ** y

ผู้ดำเนินการมอบหมายกะ

Operator Example Same As
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y

ตัวดำเนินการมอบหมาย Bitwise

Operator Example Same As
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y

ตัวดำเนินการกำหนดตรรกะ

Operator Example Same As
&&= x &&= y x = x && (x = y)
||= x ||= y x = x || (x = y)
??= x ??= y x = x ?? (x = y)

บันทึก

ตัวดำเนินการกำหนดตรรกะคือ ES2020


ตัวดำเนินการ =

Simple Assignment Operator กำหนดค่าให้กับตัวแปร

ตัวอย่างการมอบหมายอย่างง่าย

let x = 10;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>
let x = 10 + y;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>

<p id="demo"></p>

<script>
let y = 50
let x = 10 + y;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ +=

ตัวดำเนินการกำหนดการเพิ่ม จะเพิ่มค่าให้กับตัวแปร

ตัวอย่างการมอบหมายเพิ่มเติม

let x = 10;
x += 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>
let text = "Hello";
text += " World";

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Addition Assignment</h2>
<h3>The += Operator</h3>

<p id="demo"></p>

<script>
let text = "Hello";
text += " World";
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

ตัวดำเนินการ -=

ตัวดำเนินการกำหนดการลบ จะลบค่าออกจากตัวแปร

ตัวอย่างการกำหนดการลบ

let x = 10;
x -= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Subtraction Assignment</h2>
<h3>The -= Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x -= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ *=

ตัวดำเนินการกำหนดสูตรคูณ จะคูณตัวแปร

ตัวอย่างการคูณการคูณ

let x = 10;
x *= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Multiplication Assignment</h2>
<h3>The *= Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x *= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

**= ตัวดำเนินการ

ตัวดำเนินการกำหนดเลขชี้กำลัง เพิ่มตัวแปรตามกำลังของตัวถูกดำเนินการ

ตัวอย่างการมอบหมายการยกกำลัง

let x = 10;
x **= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Exponentiation Assignment</h2>
<h3>The **= Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x **= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ /=

ตัวดำเนินการกำหนดแผนก แบ่งตัวแปร

ตัวอย่างการมอบหมายแผนก

let x = 10;
x /= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Division Assignment</h2>
<h3>The /= Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

ตัวดำเนินการ %=

ตัวดำเนินการกำหนดส่วนที่เหลือ กำหนดส่วนที่เหลือให้กับตัวแปร

ตัวอย่างการมอบหมายส่วนที่เหลือ

let x = 10;
x %= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Remainder Assignment</h2>
<h3>The %= Operator</h3>

<p id="demo"></p>

<script>
let x = 10;
x %= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>


<<= ตัวดำเนินการ

ตัวดำเนินการกำหนด Shift ซ้าย เลื่อนตัวแปรไปทางซ้าย

ตัวอย่างการมอบหมายกะซ้าย

let x = -100;
x <<= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Left Shift Assignment</h2>
<h3>The &lt;&lt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x <<= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

>>= ผู้ดำเนินการ

ตัวดำเนินการกำหนด Shift ขวา เลื่อนตัวแปรทางขวา (ลงนาม)

ตัวอย่างการมอบหมายกะขวา

let x = -100;
x >>= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The &gt;&gt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x >>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

>>>= ผู้ดำเนินการ

ตัวดำเนินการกำหนด Shift ขวาที่ไม่ได้ลงนาม เลื่อนตัวแปรทางขวา (ไม่ได้ลงนาม)

ตัวอย่างการมอบหมายกะขวาที่ไม่ได้ลงนาม

let x = -100;
x >>>= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Right Shift Assignment</h2>
<h3>The &gt;&gt;&gt;= Operator</h3>

<p id="demo"></p>

<script>
let x = -100;
x >>>= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ &=

Bitwise AND Assignment Operator ดำเนินการ bitwise AND กับตัวถูกดำเนินการสองตัว และกำหนดผลลัพธ์ให้กับตัวแปร

Bitwise และตัวอย่างการมอบหมาย

let x = 10;
x &= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Bitwise AND Assignment</h2>
<h3>The &amp;= Operator</h3>

<p id="demo"></p>

<script>
let x = 100;
x &= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ |=

ตัวดำเนินการ Bitwise OR Assignment ทำ bitwise OR การดำเนินการกับตัวถูกดำเนินการสองตัว และกำหนดผลลัพธ์ให้กับตัวแปร

Bitwise หรือตัวอย่างการมอบหมาย

let x = 10;
x |= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Bitwise OR Assignment</h2>
<h3>The |= Operator</h3>

<p id="demo"></p>

<script>
let x = 100;
x |= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ ^=

ตัวดำเนินการกำหนด XOR ระดับบิต ทำการดำเนินการระดับบิต XOR กับตัวถูกดำเนินการสองตัว และกำหนดผลลัพธ์ให้กับตัวแปร

ตัวอย่างการกำหนด Bitwise XOR

let x = 10;
x ^= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Bitwise XOR Assignment</h2>
<h3>The ^= Operator</h3>

<p id="demo"></p>

<script>
let x = 100;
x ^= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ &&=

ตัวดำเนินการเชิงตรรกะและการกำหนด ถูกใช้ระหว่างสองค่า

หากค่าแรกเป็นจริง ค่าที่สองจะถูกกำหนดค่า

ตัวอย่างเชิงตรรกะและการมอบหมาย

let x = 10;
x &&= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<h3>The &amp;&amp;= Operator</h3>
<p>If the first value is true, the second value is assigned.</p>

<p id="demo"></p>

<script>
let x = 100;
x &&= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ &&= เป็นคุณลักษณะ ES2020


ตัวดำเนินการ ||=

ตัวดำเนินการเชิงตรรกะหรือการกำหนด ถูกใช้ระหว่างสองค่า

ถ้าค่าแรกเป็นเท็จ ค่าที่สองจะถูกกำหนดค่า

ตัวอย่างเชิงตรรกะหรือการกำหนด

let x = 10;
x ||= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>Logical OR Assignment</h2>
<h3>The ||= Operator</h3>

<p>If the first value is false, the second value is assigned:</p>
<p id="demo"></p>

<script>
let x = undefined;
x ||= 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>

</body>
</html>

ตัวดำเนินการ ||= เป็นคุณลักษณะ ES2020


ตัวดำเนินการ ??=

ตัวดำเนินการกำหนดการรวม Nullish ถูกใช้ระหว่างสองค่า

หากค่าแรกไม่ได้กำหนดหรือเป็นค่าว่าง ค่าที่สองจะถูกกำหนดค่า

ตัวอย่างการมอบหมายการรวมศูนย์แบบ Nullish

let x;
x ??= 5;

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Assignments</h1>
<h2>The ??= Operator</h2>
<p>The ??= operator is used between two values. If the first value is undefined or null, the second value is assigned.</p>

<p id="demo"></p>

<script>
let x;
document.getElementById("demo").innerHTML = x ??= 5; 
</script>

</body>
</html>

ตัวดำเนินการ ??= เป็นคุณลักษณะ ES2020