JavaScript Assignment
JavaScript Assignment Operators (Configuration Operators) are the markers used to configure to variables in the JavaScript language.
The most common operator is the single equals sign (=), which assigns a right value to the variable on the left.
Operator Name Example Meaning
Assignment x = y requires that x is equal to y.
+ = Addition Assignment x + = y is equivalent to: x = x + y
- = Subtraction Assignment x - = y is equivalent to: x = x - y.
☆ = Multiplication Assignment x ☆ = y is equivalent to: x = x ☆ y
/ = Division Assignment x / = y is equivalent to: x = x / y.
% = Remainder Assignment x% = y is equivalent to: x = x% y (rest from division).
$
Export to sheet
Compound Assignment Operators
In addition to =, JavaScript also provides abbreviated configuration operators (such as + =, - =, etc.) that combine mathematical or bitwise operations (bitwise) with the configuration, which allows more concise and easier to read code.
Example of use
Suppose the variable x has a default value of 10:
JavaScript
Let x = 10;
Let y = 5;
/ / Use of + = (Addition Assignment)
x + = y; / / is equivalent to x = x + y; (x will be 15)
/ / Usage - = (Subtraction Assignment)
x - = 3; / / is equivalent to x = x - 3; (x will be 12)
/ / Usage * = (Multiplication Assignment)
x * = 2; / / is equivalent to x = x * 2; (x will be 24)
Note: There are also other configuration operators for bitwise operations (Bitwise Assignment Operators), such as < < =, > > =, $& = $, ️ =, and =.
นอกจากตัวดำเนินการกำหนดค่าที่อธิบายในบทความแล้ว ยังมีหลายเคล็ดลับที่ช่วยให้การเขียน JavaScript ของคุณมีประสิทธิภาพมากขึ้น เช่น การใช้ตัวดำเนินการกำหนดค่าแบบย่อช่วยลดความซับซ้อนของโค้ดและทำให้โค้ดอ่านง่ายขึ้นจริงๆ ในชีวิตจริง ผมมักใช้ += เพื่อเพิ่มค่าตัวแปรอย่างรวดเร็ว รวมถึงใช้ **= สำหรับการคำนวณยกกำลังซึ่งมีประโยชน์มากเมื่อทำงานกับตัวเลขที่ต้องการการคำนวณซับซ้อนยกตัวอย่างเช่น การคำนวณพลังของฟังก์ชันทางคณิตศาสตร์ นอกจากนี้ การเข้าใจตัวดำเนินการกำหนดค่าแบบบิตไวส์ (bitwise assignment operators) เช่น <<=, >>=, &=, ^=, |= ก็ช่วยได้มากเมื่อทำงานกับข้อมูลที่ต้องใช้การจัดการบิต เช่นในงานด้านเครือข่ายหรืองานที่ต้องจัดการข้อมูลระดับบิตโดยตรง สุดท้าย อย่าลืมว่าเครื่องมือดีๆ เหล่านี้มีประโยชน์มากเมื่อเราเข้าใจความหมาย การนำไปใช้ และสถานการณ์ที่เหมาะสม ซึ่งจะส่งผลให้โค้ด Javascript ของคุณมีคุณภาพและง่ายต่อการบำรุงรักษา



















