Mathematical operator in javay
Arithmetic Operators in JavaScript are the signs used to compute mathematics between operands to get a new number. The basic ones used are as follows:
Operator English name description sample result
+ Addition 10 + 5 15
- Subtraction, deletion 10 - 5 5
* Multiplication multiplication 10 * 5 50
/ Division division 10 / 5 2
% Modulo division took the remainder 10% 3 1
* * Exponentiation 2 * * 3 8
Export to sheet
Increase / Decrement Operators
These operators are used to increase or decrease the variable value in increments of 1.
Operator, description, sample, result (of x)
+ + add 1 increment, let x = 5; x + + 6.
- reduce it by 1, let y = 5; y-- 4.
Export to sheet
Note: The + + and -- operators have a use pattern before the variable (prefix: + + x) and after the variable (postfix: x + +), which affects the value returned immediately in that line of processing.
Compound Assignment Operators
It is a shortened form of mathematical operation and configuration back to the original variable.
The example operator is equivalent to a description.
+ = a + = 5a = a + 5 Add the value and then define it back.
- = b - = 2 b = b - 2 subtract the value and then define it back.
* = c * = 3 c = c * 3 times the value and then define it back.
/ = d / = 4 d = d / 4 Divide the value and define it back.
% = e% = 2 e = e% 2, divide the numerator and set it back.
* * = f * * = 3 f = f * * 3 raised to power and then defined back.
Export to sheet
💡 Caution to use the positive operator (+)
When using the + operator in conjunction with String (text) data, JavaScript operates as a text concatenation instead of mathematical addition, such as:
JavaScript
console.log (5 + 5); / / Result: 10 (plus numbers)
console.log ("Hello" + "World"); / / Result: "Hello World" (text welding)
console.log ("Value:" + 10); / / Result: "Value: 10" (Weld text and numbers)
Arithmetic Operators in JavaScript are particularly important because they are the foundation of all numerical data processing in programming. These are the main reasons why they are important:
1. Data calculation and processing 🧠
Mathematical operators are indispensable tools for building computationally related programs, such as:
Financial information management: adding, subtracting, multiplying, dividing to calculate the total, discount, tax, or interest calculation.
In-Game Data Management: Score Calculation (Addition), Life Force Management (Subtraction), or Bonus Multiplication
Statistical Processing: Averaging, or Managing Numbers in Data Tables
Formatting (Layout): At times used to calculate the size (width / height) or position (position) of an element on a web page.
2. Flow Control 🚦
Despite being a mathematical operator, its calculations affect the control of the direction of the code. Specifically:
Loops: Increment (+) and Decrement (--) operators are at the heart of the loop for increasing or decreasing the counter in the loop.
JavaScript
for (let i = 0; i < 10; i + +) {/ / Use i + + to add a counter.
/ /....Repeat 10 times.
}
Condition validation: Results from mathematical calculations are often combined with Comparison Operators in the if / else statement to decide what action to take next.
3. Ease and efficiency of writing code ✨
Compound Assignment Operators: e.g. + =, - =, * = Allows shorter code writing, easier reading, and reduces the chance of reprinting errors.
JavaScript
score + = 10; / / shorter score = score + 10;
Modulo Operator,%: Important for checking whether the number is even or odd, grouping the data, or limiting the result to a certain scope, such as the rotation of the number.
In summary, mathematical operators are the basic language that programmers communicate to computers how to manipulate and change numerical values, allowing programs to perform complex tasks.
นอกจากตัวดำเนินการทางคณิตศาสตร์พื้นฐานแล้ว JavaScript ยังมีตัวดำเนินการตรรกะ (Logical Operators) ที่ช่วยให้การตัดสินใจในการเขียนโปรแกรมสมบูรณ์ขึ้น เช่น && (AND), || (OR) และ ! (NOT) ซึ่งมักใช้ร่วมกันกับตัวเปรียบเทียบเพื่อจัดการกับเงื่อนไขต่างๆ ตัวอย่างการใช้ตัวดำเนินการตรรกะเพื่อเพิ่มประสิทธิภาพในการค้นหาข้อมูลหรือการตรวจสอบเงื่อนไขต่างๆ ในโปรแกรม เช่น การตรวจสอบว่าตัวเลขอยู่ในช่วงที่กำหนดหรือไม่ หรือการเช็คสถานะของตัวแปรก่อนทำงานในโค้ด การใช้ตัวดำเนินการอย่างถูกต้องช่วยลดข้อผิดพลาดและเพิ่มความเข้าใจให้กับคนอ่านโค้ด โดยเฉพาะการใช้ตัวดำเนินการร่วม เช่น += หรือ -= ที่ช่วยให้เขียนโค้ดสั้นลงและดูเรียบร้อยมากขึ้น สุดท้าย อย่าลืมว่าการเรียนรู้การใช้ตัวดำเนินการใน JavaScript อย่างลึกซึ้ง จะทำให้คุณสามารถสร้างโปรแกรมที่ซับซ้อนและมีประสิทธิภาพ พร้อมกับเข้าใจวิธีทำงานของโค้ดในระดับลึกขึ้น ส่งผลให้สามารถแก้ไขปัญหาและพัฒนาโปรแกรมได้อย่างรวดเร็วและถูกต้องมากขึ้น




































































































