How to Add Numbers in JavaScript17 Mar 2025 | 3 min read We will understand how to add numbers in JavaScript in this article. JavaScript NumbersIn JavaScript, the numbers are stored in double-precision 64-bit binary format IEEE 754. Methods utilized to add two numbersFollowing are the methods that we can utilize to add two numbers in JavaScript:
We will understand each method one by one. Utilizing the + operatorThe + operator is utilized to add numbers in JavaScript. Demonstration:We will utilize the + operator for adding two numbers. Code: Output: We can witness that the two numbers are added. ![]() Utilizing functionWe will utilize a function to add numbers in JavaScript. Demonstration:We will define a function, give it a name and add two numbers. Code: Output: We can witness the addition of two numbers utilizing a function in the output downwards. ![]() Utilizing arrow functionWe will define an arrow function to add two numbers. This function is utilized to add parameters and returns the sum. Syntax of arrow function: Code: Output: Here in the output downwards we can witness the addition of two numbers utilizing an arrow function. ![]() Utilizing addition assignment (+=) operatorWe will utilize the addition assignment (+=) operator. This operator sums up the left and right operand values. After that, it gives the addition result to the left operand. Code: Output: Here is the output in which we can witness the utilization of addition assignment operator to get the sum of two numbers. ![]() Addition of floating-point numbersFloating-point numbers are decimal numbers that contain decimal points, such as 2.421. Demonstration-1:We will add two floating-point numbers in this demo. Code: In the above JavaScript code, "a" and "b" are two variables that contains floating-point value. The "c" is another variable that stores the result of sum of "a" and "b". Output: We can witness that the output of "a" and "b" is 8.151 which means decimal points are retained. ![]() We can utilize the "toFixed" function to control the decimal point number. Syntax: toFixed() is the function and 3 is the value up to which we want to fix the number of decimal points. Demonstration-2:We will add two numbers and utilize the toFixed() function to control the decimal points. Code: Output: We can clearly witness that the decimal point is fixed to 2 even if we have utilized a 3-decimal point number. ![]() Demonstration-3:We can get the result of adding two numbers with more precision which means we can round numbers with the help of the Math.round() method. Code: Output: We can witness that the decimal point is removed and we have got the round number. ![]() Conclusion:We have understood how to add numbers in JavaScript in this article. We can add numbers utilizing + operator, utilizing function, utilizing arrow function and utilizing addition assignment (+=) operator. Next TopicJavascript-logical-operators |
In this section, we will discuss the print() method in the JavaScript language. A print() method is used to print the currently visible contents like a web page, text, image, etc., on the computer screen. When we use a print() method in JavaScript and execute...
4 min read
JavaScript has made some amazing progress since its beginning during the 1990s. From its unassuming starting points as a basic scripting language for adding intuitiveness to web pages, JavaScript has developed into a strong and flexible language that drives current web development. With the ascent...
12 min read
We will understand the composition in JavaScript in this article. As the word "composition" means combining two or more things together to compose a new thing. Function Composition In JavaScript, there is a technique called the function composition. This technique is utilized to combine two or more functions...
3 min read
? A necessary part of strong programming advancement is the means by which to handle errors. Throwing errors is a way to handle and respond to unexpected conditions in your JavaScript code. Coders who know how to throw errors can foster code that is simpler to use...
5 min read
ES6 offers a metaproperty called new.target to identify whether a function or constructor runs using the new operator. The target attribute, dot, and new keyword build the object of the new.target. All methods have access to the new.target metaproperty. In contrast, the new.target in arrow functions...
3 min read
The eval() function in JavaScript is used to evaluate the expression. It is JavaScirpt's global function, which evaluates the specified string as JavaScript code and executes it. The parameter of the eval() function is a string. If the parameter represents the statements, eval() evaluates the statements....
2 min read
What is JavaScript? JavaScript is a very powerful and popular programming language with a lot of applications in terms of web development. It enables developers to design dynamic and interactive content for web pages for a seamless user experience. What is an Array? An array is a...
4 min read
Overview In JavaScript, an array is a global object type used to store data. An array is an ordered list or collection of zero or more data types that can be accessed by numbered indices beginning at 0. Because arrays allow us to store multiple values in...
5 min read
What is Sandbox in JavaScript? In JavaScript, a sandbox is a security implementation that helps us limit the capabilities of the JavaScript code running inside it. It is regularly used in the user and server-side environment to execute the code that cannot be relied on and ent...
4 min read
JavaScript offers some built-in date and time functions, which helps to calculate the age from the date (Date of Birth). Using these JavaScript methods, you can easily find the age of anyone. For this, we require a date input from the user and the current...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India