Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

program addition of two numbers in java script

ramprakashd
Tera Contributor

please explain addition of two numbers in java script in service now and i need a code for that.

2 REPLIES 2

Sid_Takali
Kilo Patron

Hi @ramprakashd In JavaScript, adding two numbers is straightforward. You just use the + operator. For example:

var num1 = 5;
var num2 = 10;
var sum = num1 + num2;
gs.info("The sum is: " + sum);  // gs.info is used  to log messages

In business rule you can write like this 

(function executeRule(current, previous /*null when async*/) {

    var num1 = 5;
    var num2 = 10;
    var sum = num1 + num2;
    gs.addInfoMessage("The sum of " + num1 + " and " + num2 + " is: " + sum);

})(current, previous);

 

Learn JavaScript from scratch 

https://www.codecademy.com/learn/introduction-to-javascript 

https://www.w3schools.com/js/  

Sandeep Rajput
Tera Patron
Tera Patron

@ramprakashd This is a very basic question, the script varies depending on from where you are executing.

 

Here is an example of a client script

function onLoad(){
var num1 = 10;
    var num2 = 20;
    
    // Adding the numbers
    var sum = addNumbers(num1, num2);
    
    // Display the result
    alert('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
}

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