- Increment (++)
- Try it
- Syntax
- Description
- Examples
- Postfix increment
- Prefix increment
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Addition (+)
- Try it
- Syntax
- Description
- Examples
- Number addition
- BigInt addition
- String concatenation
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- Add Two Numbers in JavaScript
- Add Two Numbers in JavaScript
- JavaScript Program to Add Two Numbers in HTML
- JavaScript Program to Add Two Numbers Using Form and TextBox
- Get User Input One by One
- Conclusion
- Increment (++)
- Try it
- Syntax
- Description
- Examples
- Postfix increment
- Prefix increment
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
Increment (++)
The increment ( ++ ) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.
Try it
Syntax
Description
The ++ operator is overloaded for two types of operands: number and BigInt. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt increment if the operand becomes a BigInt; otherwise, it performs number increment.
If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing.
The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). ++x itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.
++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation
Examples
Postfix increment
let x = 3; const y = x++; // x is 4; y is 3 let x2 = 3n; const y2 = x2++; // x2 is 4n; y2 is 3n
Prefix increment
let x = 3; const y = ++x; // x is 4; y is 4 let x2 = 3n; const y2 = ++x2; // x2 is 4n; y2 is 4n
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Mar 28, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Addition (+)
The addition ( + ) operator produces the sum of numeric operands or string concatenation.
Try it
Syntax
Description
The + operator is overloaded for two distinct operations: numeric addition and string concatenation. When evaluating, it first coerces both operands to primitives. Then, the two operands’ types are tested:
- If one side is a string, the other operand is also converted to a string and they are concatenated.
- If they are both BigInts, BigInt addition is performed. If one side is a BigInt but the other is not, a TypeError is thrown.
- Otherwise, both sides are converted to numbers, and numeric addition is performed.
String concatenation is often thought to be equivalent with template literals or String.prototype.concat() , but they are not. Addition coerces the expression to a primitive, which calls valueOf() in priority; on the other hand, template literals and concat() coerce the expression to a string, which calls toString() in priority. If the expression has a @@toPrimitive method, string concatenation calls it with «default» as hint, while template literals use «string» . This is important for objects that have different string and primitive representations — such as Temporal, whose valueOf() method throws.
const t = Temporal.Now.instant(); "" + t; // Throws TypeError `$t>`; // '2022-07-31T04:48:56.113918308Z' "".concat(t); // '2022-07-31T04:48:56.113918308Z'
You are advised to not use «» + x to perform string coercion.
Examples
Number addition
// Number + Number -> addition 1 + 2; // 3 // Boolean + Number -> addition true + 1; // 2 // Boolean + Boolean -> addition false + false; // 0
BigInt addition
// BigInt + BigInt -> addition 1n + 2n; // 3n // BigInt + Number -> throws TypeError 1n + 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions // To add a BigInt to a non-BigInt, convert either operand 1n + BigInt(2); // 3n Number(1n) + 2; // 3
String concatenation
// String + String -> concatenation "foo" + "bar"; // "foobar" // Number + String -> concatenation 5 + "foo"; // "5foo" // String + Boolean -> concatenation "foo" + false; // "foofalse" // String + Number -> concatenation "2" + 2; // "22"
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Mar 28, 2023 by MDN contributors.
Your blueprint for a better internet.
Add Two Numbers in JavaScript
JavaScript program to add two numbers is one of the fundamental programs that every beginner comes across. After learning fundamental concepts of JavaScript like variables , and functions , adding two numbers would be the best way to implement them. We can add two numbers in JavaScript by using the addition operator +. The Addition operator takes two operands and returns their sum.
Add Two Numbers in JavaScript
Let us see the easiest way of adding two numbers in JavaScript
Let us understand the above JavaScript code; Here, we have initialized two variables, num1 and num2 . We assigned 10 to num1 and 30 to num2 . Now the statement sum gets initialized with num1+num2 (10 + 30) , so the sum of the two numbers 10 and 30 will be assigned to the sum variable. The value 40 will be assigned to the sum variable as a result sum operation.
JavaScript Program to Add Two Numbers in HTML
Let us see how to write JavaScript code that adds two numbers and displays the result in a web browser. To display the result in a web browser, we write the code in a .html file.
Now Save the above code with the .html extension. Go to any web browser and open the recently saved .html file. We could see that the browser displays the sum of two numbers on the screen.
Explanation :
We have taken two variables as num1 and num2 and initialized them with values 10 , and 40 , respectively, the sum of these two variables is stored in the sum variable. The result of this sum operation is displayed in the web browser.
JavaScript Program to Add Two Numbers Using Form and TextBox
Let us see the actual JavaScript program that lets the user give the input using Form and Textbox.
Now, we can see that browser displays two text boxes where one text box is for taking input of num1 , and another textbox is for taking input of num2 . We will provide values of num1, num2 and then click on the add button, which displays the sum of two numbers.
Explanation:
In the above code, we have taken two HTML input elements which are used for capturing the input numbers. These two numbers have the id of firstnumber and secondnumber , respectively (we will be using these ids at a later stage). We have also taken a button to call the add function after taking the inputs.
Now that we have taken inputs and clicked on add button the flow goes to add a function which is written in a script tag, In the script tag, we have taken three variables as num1, num2 , and sum.
Here, num1 and num2 will be used for storing the previously taken two input values. The interesting thing is that the line parseInt(document.getElementById(«firstnumber»).value) in JavaScript will help us to get hold of firstnumber, In the above line replacing the firstnumber with secondnumber will help us to get hold of secondnumber. By this point, we got our two input numbers stored into num1 , num2 . So now we will compute the sum using num1+num2
We have taken a HTML element with just input >, using this input id we display the sum on the browser, i.e. d ocument.getElementById(«answer»).value = sum ; this line captures the answer id and writes to its value property.
Get User Input One by One
Let us look at one more method for JavaScript Program to add two numbers.
This method looks much similar to the above-discussed method.
Major difference for this implementation is that, here user would give input for only one element at a time.
Explanation:
Just like the previous implementation, we have taken two HTML input elements, but the catch is that we will not display both the HTML input elements at a time, we display one field at a time, and after giving the user input and clicking on enter we display the next HTML input element then we click add so that sum of the two numbers would be displayed.
For the second HTML input element with p > we have added style attribute as style=»display:none;» this ensures that this field will not be displayed. We have done the same thing with p >.
Once the user enters a number and clicks on Enter button, the FirstNum() function gets called, and here we get the number by using its id and store it in num1. Now, we check if the number is a true value, if it is true, then by using internal css, we hide the blockOne HTML field by setting temp.style.display = «none» ; and display the blockTwo HTML field by setting temp.style.display = «block» .
Now we give the input for the second number and click on add so that SecondNum() function gets called, and we get the number by using its id and store it in num2 .
In the SecondNum() function, we check if both num1 and num2 are true or not, if both are true then we hide the secondInput by setting temp.style.display = «none» . Now we will compute sum and display it on browser by using blockThree , as it was hidden, we will display it by setting temp.style.display = «block» ; and display the result by document.getElementById(«res»).innerHTML = sum .
Here, we enter 7 as the first number and then 8 as the second number, thus getting 15 as the sum.
Conclusion
- As stated previously, adding two numbers in JavaScript is a program that every beginner faces because it clarifies many doubts regarding how to use JavaScript functions, storing sum in a different variable.
- We use the addition operator + to add two numbers in JavaScript.
- In javaScript, we can use various methods to add two numbers, like using JavaScript functions, form and textboxes.
- In javascript, we can take only one input from the user at a time and add them to return the result.
Increment (++)
The increment ( ++ ) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.
Try it
Syntax
Description
The ++ operator is overloaded for two types of operands: number and BigInt. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt increment if the operand becomes a BigInt; otherwise, it performs number increment.
If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing.
The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). ++x itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.
++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation
Examples
Postfix increment
let x = 3; const y = x++; // x is 4; y is 3 let x2 = 3n; const y2 = x2++; // x2 is 4n; y2 is 3n
Prefix increment
let x = 3; const y = ++x; // x is 4; y is 4 let x2 = 3n; const y2 = ++x2; // x2 is 4n; y2 is 4n
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Mar 28, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.