Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Program to Add Two Numbers
Next article icon

JavaScript Program to Add Two Numbers

Last Updated : 08 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We will explore how to add two numbers in JavaScript. Adding two numbers in JavaScript means combining numeric values together using arithmetic addition, which gives us their total sum.

There are several methods that can be used to Add Two Numbers in JavaScript, which are listed below:

Table of Content

  • Using + Operator
  • Using function
  • Using Arrow function
  • Using Addition Assignment (+=) Operator

Using + Operator

In this approach we add two numbers in JavaScript involves using the + operator to perform arithmetic addition on numeric variables, resulting in their sum.

Syntax:

x + y;

Example: In this example, we are adding two numeric values by using the + operator.

JavaScript
let num1 = 10; let num2 = 10; let sum = num1 + num2; console.log("Sum :", sum); 

Output
Sum : 20 

Using function

In this approach, we are adding two numbers using a function in JavaScript involves defining a custom function that takes two parameters, adds them, and returns the result.

Syntax:

function additionFunction(a, b) {     return a + b; }

Example: In this example we are using the above-explained approach.

JavaScript
function additionFunction(a, b) {     return a + b; }  let num1 = 5; let num2 = 10; let sum = additionFunction(num1, num2); console.log("Sum of given numbers is :", sum); 

Output
Sum of given numbers is : 15 

Using Arrow function

Adding two numbers using an arrow function in JavaScript involves creating a concise function syntax that adds parameters and returns the sum.

Syntax:

let addition = (a, b) => a + b;

Example: In this example we are using arrow function to add two numbers.

JavaScript
let addition = (a, b) => a + b;  let num1 = 25; let num2 = 25; let sum = addition(num1, num2); console.log("Sum of given numbers is :", sum); 

Output
Sum of given numbers is : 50 

Using Addition Assignment (+=) Operator

In this approach we use Addition Assignment (+=) operator in which operator Sums up left and right operand values and then assigns the result to the left operand.

Syntax:

Y += 1 gives Y = Y + 1 

Example: In this example we are using the above-explained approach.

JavaScript
let num1 = 15; let num2 = 10;  // Equivalent to num1 = num1 + num2 num1 += num2; console.log("Sum of the given number is :", num1); 

Output
Sum of the given number is : 25 

Next Article
JavaScript Program to Add Two Numbers

V

vishalkumar2204
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-math
  • JavaScript-Questions

Similar Reads

    Javascript Program For Adding 1 To A Number Represented As Linked List
    Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Below are the steps : Reverse given linked list. For example, 1->
    5 min read
    PHP Program to Add Two Numbers
    Given two numbers, the task is to add two numbers in PHP. It is one of the most basic arithmetic operations and a fundamental concept in programming. Examples:Input: num1 = 10, num2 = 20Output: sum = 30Input: num1 = 70, num2 = 120Output: sum = 190Table of Content Using + operatorUsing Arrays and Arr
    3 min read
    JavaScript Program to Add Two Binary Strings
    Here are the various ways to add two binary stringsUsing parseInt() and toString() The parseInt() method used here first converts the strings into the decimal. Ten of these converted decimal values are added together and by using the toString() method, we convert the sum back to the desired binary r
    4 min read
    JavaScript Program for Multiplication of Two Numbers
    In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer
    4 min read
    JavaScript Program to Perform Simple Mathematical Calculation
    In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 :
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences