In JavaScript programming, performing arithmetic operations is an important and fundamental part. By using operators and arithmetic functions in JavaScript, you can perform addition, subtraction, multiplication, division, and more.
JavaScript provides some basic operators to perform arithmetic operations.
Addition Operator (+): Used to add two values together.
let sum = 5 + 3; // sum = 8
Subtraction Operator (-): Used to subtract one value from another.
let difference = 10 - 4; // difference = 6
Multiplication Operator (*): Used to multiply two values together.
let product = 2 * 6; // product = 12
Division Operator (/): Used to divide one value by another.
let quotient = 15 / 3; // quotient = 5
Modulus Operator (%): Used to get the remainder after dividing one value by another.
let remainder = 17 % 4; // remainder = 1
Here are some examples of using operators and arithmetic functions in JavaScript:
// Ví dụ về toán tử cộng
let sum = 5 + 3; // sum = 8
// Ví dụ về toán tử trừ
let difference = 10 - 4; // difference = 6
// Ví dụ về toán tử nhân
let product = 2 * 6; // product = 12
// Ví dụ về toán tử chia
let quotient = 15 / 3; // quotient = 5
// Ví dụ về toán tử chia lấy phần dư
let remainder = 17 % 4; // remainder = 1
// Ví dụ về hàm tính toán
let absoluteValue = Math.abs(-8); // absoluteValue = 8
// Ví dụ về hàm tính căn bậc hai
let squareRoot = Math.sqrt(25); // squareRoot = 5
// Ví dụ về hàm tính lũy thừa
let power = Math.pow(2, 3); // power = 8
// Ví dụ về hàm làm tròn số xuống
let roundedNumber = Math.floor(4.9); // roundedNumber = 4
// Ví dụ về hàm tạo số ngẫu nhiên
let randomNumber = Math.random(); // randomNumber = 0.745891234