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 |
What is the definition of type conversion? When a value is transformed from one type to the other, it is called type conversion. The types of values in JavaScript are diverse. One could possess a numerical value, a string, an object, a Boolean, or any combination. To fit...
6 min read
There is full control to handle loop statements in JavaScript. Sometimes, a situation occurs when we require to skip some code of the loop and move to the iteration. It can be achieved by using JavaScript's continue statement. The continue statement in JavaScript is used...
3 min read
Introduction: Merging arrays in JavaScript is a basic idea in data manipulation, allowing developers to turn disorderly data from many sources into a standard cohesive unit. Merging arrays is a routine and important operation used for one thing or another (data aggregation, simplifying code, etc.) and serves...
5 min read
Introduction Designing websites has been implemented by a lot of tech companies where the programming language JavaScript has become efficient usage. Its inherent single-threading is one of its fundamental qualities, despite its diversity. JavaScript runs code in a single-threaded environment, in contrast to languages like C++ or...
7 min read
When it comes to web development, JavaScript is one of the most popular and adaptable programming languages. Because of its adaptability, it is a fundamental component of contemporary web development, enabling developers to create dynamic and interactive web pages. The ability to extract meaning from code...
9 min read
In programming, camel case is a common writing style where the first word begins with a lowercase letter, and each subsequent word begins with an uppercase letter. This naming convention is particularly used in JavaScript for variables, functions, and methods. Variables like getUserInfo, fetchDataFromAPI, and userName...
8 min read
Introduction JavaScript, being one of the most popular languages across the galaxy of programming languages is considered as the core component in fulfilling all the tasks that developers must perform. JavaScript has evolved as a language that started from simple page scripts to complex web applications, APIs,...
20 min read
JavaScript is a high level and versatile programming language primarily used for web development. It has grown over time to be more powerful, ultimately becoming a sophisticated programming language strong enough to drive full-fledged applications. This article takes a deep dive into the top 10 use...
9 min read
Introduction: Navigating Object Iteration with forEach() in JavaScript: With its flexible and life-like characteristics, JavaScript developers can develop outstanding and interactive web applications. Whether you're an advanced developer or it's your first attempt into JavaScript, being able to iterate over data structures successfully is essential for...
20 min read
The interactive features of webpages are powered by JavaScript, a flexible and popular computer language. HackerRank offers an ideal environment to practise and advance your JavaScript competence, regardless of your level of expertise. Whether you are a novice trying to understand the principles or an...
9 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