Numbers and maths · Angka dan matematika
Maths like a calculator
- JavaScript does maths with
+-*/. - There is only one kind of number, so
/can give a decimal:7 / 2is3.5.
Matematika seperti kalkulator
- JavaScript melakukan matematika dengan
+-*/. - Hanya ada satu jenis angka, jadi
/dapat menghasilkan desimal:7 / 2adalah3.5.
console.log(7 + 2); // → 9
console.log(7 / 2); // → 3.5
Remainder and powers
%gives the remainder after dividing:23 % 5is3(5 goes in 4 times, 3 left over).**raises to a power:2 ** 5is2 × 2 × 2 × 2 × 2.
Sisa bagi dan pangkat
%memberikan sisa bagi setelah pembagian:23 % 5adalah3(5 masuk 4 kali, sisa 3).**mengangkat ke pangkat:2 ** 5adalah2 × 2 × 2 × 2 × 2.
console.log(23 % 5); // → 3
console.log(2 ** 5); // → 32
Order of operations
*/%happen before+and-— the same rules as in maths class.- Use brackets
( )to force what should happen first.
Urutan operasi
*/%terjadi sebelum+dan-— aturan yang sama seperti di kelas matematika.- Gunakan kurung
( )untuk memaksa apa yang harus terjadi terlebih dahulu.
Order of operations, step by step · Urutan operasi, langkah demi langkah
× and ÷ happen before + and −. Brackets go first of all. · × dan ÷ terjadi sebelum + dan −. Kurung dilakukan paling awal.
console.log(2 + 3 * 4); // → 14 (the 3 * 4 first)
console.log((2 + 3) * 4); // → 20 (the brackets first)
Common mistakes
%is the remainder operator;/can give a decimal."3" + 4gives"34"(a string), not7.
Kesalahan umum
%adalah operator sisa bagi;/dapat menghasilkan desimal."3" + 4memberikan"34"(string), bukan7.
Now you try
- Use the maths operators.
- Press Check answer to test it.
Sekarang Anda coba
- Gunakan operator matematika.
- Tekan Cek jawaban untuk mengujinya.
Log 6 times 7 (it is 42). · Log 6 kali 7 (yaitu 42).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use % to log the remainder when 23 is divided by 5 (it is 3). · Gunakan % untuk log sisa pembagian 23 oleh 5 (yaitu 3).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
a is 4 and b is 8. Log their average, (a + b) / 2 (it is 6). Use brackets so the addition happens first. · a adalah 4 dan b adalah 8. Log rata-ratanya, (a + b) / 2 (yaitu 6). Gunakan kurung agar penjumlahan dilakukan terlebih dahulu.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.