GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing flatcoding.com — Understanding JavaScript Arithmetic Operators - FlatCoding Home Understanding JavaScript Arithmetic Operators
Table of ContentJavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or increase values. What are JavaScript Arithmetic OperatorsJavaScript arithmetic operators are symbols that let you do math with numbers. They include basic operations like addition ( Here are the basic operators:
Let’s take a look at each one in depth. The Addition Operator (+) in JavaScriptThe addition operator ( When both values are numbers, You can also use variables: Output: 14 If at least one value is a string, Output: Hello FlatCoding Students! It works with numbers and strings: Output: The answer is 42 If one value is a string, JavaScript turns the other into a string, no matter the order. Output: 7 apples JavaScript follows type rules. If one value is a string, it turns the other into a string too. Output: 22 You can combine multiple values: JavaScript runs left to right. So Subtraction Operator (-) in JavaScriptThe subtraction operator ( You can subtract two numbers directly. Output: 6 You can also use variables: Output: 15 If you subtract values that are not numbers, JavaScript tries to convert them. Output: { result: 7, fail: NaN }
The Output: 4 Multiplication Operator (*) in JavaScriptYou use the multiplication operator (*) to multiply two numbers. It also tries to turn other types into numbers if needed. You can multiply any two numbers: Output: 18 With variables: Output: 10 If the value looks like a number, JavaScript converts it: Output: 10 Booleans also work: Output: 10 Zero always gives zero: Output: 0 Negative numbers flip the sign: Output: -12 Division Operator (/) in JavaScriptThe division operator ( You can divide numbers directly: Output: 3 You can also divide variables: Output: 4 If the numbers do not divide evenly, you get a decimal: Output: 3.5 JavaScript tries to turn strings and other types into numbers: Output: 5 Booleans also convert: Output: 0.5 JavaScript does not crash when you divide by zero. It returns Output: Infinity If both values are zero, you get Output: NaN Modulus Operator (%) in JavaScriptThe modulus operator ( Use Output: 1 Another example: Output: 3 If the number divides evenly, the result is Output: 0 The sign of the result follows the first number: Output: -1 1 -1 You can use
Exponentiation Operator (**) in JavaScriptThe exponentiation operator ( The number on the left is the base. The number on the right is the exponent. Output: 8 You can use it the same way with variables: Output: 25 You can use decimal exponents: output: 3 Negative exponents give fractions: Output: 0.25 Exponentiation works right to left: Output: 512 Wrapping UpYou learned how JavaScript uses arithmetic operators to handle basic math and value changes. You saw how each one works with numbers, strings, booleans, and special cases. Here is a quick recap:
Thank you for reading. Click to see more JavaScript tutorials. FAQ’sWhat are JavaScript arithmetic operators?JavaScript arithmetic operators are symbols used to perform basic math operations like addition, subtraction, multiplication, and division. They also include special operations like finding remainders (modulus), incrementing or decrementing values, and raising numbers to a power (exponentiation).
How does the addition operator (+) work in JavaScript?The addition operator (+) in JavaScript adds numbers or joins strings. If both operands are numbers, they are added. If at least one operand is a string, the operator joins the values into one string. For example, "5" + 2 will result in the string "52".
What happens if I divide by zero in JavaScript?JavaScript does not throw an error when dividing by zero. Instead, it returns Infinity if dividing a positive number by zero, and -Infinity if dividing a negative number by zero. If both values are zero, the result is NaN (Not a Number).
What is the modulus operator (%) in JavaScript?The modulus operator (%) returns the remainder after division. For example, 10 % 3 will return 1 because 10 divided by 3 leaves a remainder of 1. It is often used for checking even or odd numbers and wrapping values within a fixed range.
How does the exponentiation operator (**) work in JavaScript?The exponentiation operator (**) raises a number to the power of another number. For example, 2 ** 3 equals 8 (2 raised to the power of 3). It works with both integers and decimals, and supports negative exponents, which give fractional results, like 2 ** -2 which equals 0.25.
Similar ReadsJavaScript for…in Loop: Iterating Over Objects You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do… JavaScript Garbage Collection: How it Works with Examples JavaScript removes unused memory blocks with garbage collection. It frees memory space so programs run without leaks. What is JavaScript… Understanding JavaScript Objects with Examples JavaScript Objects hold values as key and value pairs, and they help you store and access data. Understand the JavaScript… JavaScript toReversed Function with Examples JavaScript toReversed returns a new array with the reversed order of its elements. It does not change the original array… Bitwise Operators in JavaScript Bitwise operators work with binary data at the bit level in JavaScript. They process numbers in binary form and return… JavaScript valueOf Function: How it Works with Examples You will learn what the JavaScript valueOf function does and how it works. It returns the primitive value of an… JavaScript Comparison Operators: How == and === Work You need to compare values in almost every JavaScript program. This is where comparison operators come in. They check if… JavaScript Introduction: What is JavaScript? The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be… Math Functions in JavaScript with Examples The Math object gives you a set of built-in functions and constants for working with numbers in JavaScript. You do… JavaScript Syntax: The Complete Cheat Sheet JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this… onMay 3, 2025
Write a Comment
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |