How to execute my Add function in javascript

I want to execute my add function with the following Code. But I'm not getting the right syntax.

I have already tried x.add(3,4) and x().add(3,4), but that doesn't work. Please let me know where I am going wrong.


      After a function has been stored in a variable, 
      the variable can be used as a function:

    <p id="demo">


      var x = function () {

        var add = function(a, b) { return a + b };

        var mul = function(a, b) { return a * b };


      document.getElementById("demo").innerHTML = x.add(3,4);



It seems you want x to be an object. The correct syntax for that is

var x = {
  add: function(a,b){return a + b},
  mul: function(a,b){return a * b},

Or if you insist on using an IIFE (because you're doing more things in that scope than you've shown), then you'd do

var x = (function () {
  function add(a,b){return a + b}
  function mul(a,b){return a * b}

  return {add, mul};

If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value . The return value is "returned" back to the "caller":

Functions defined within other functions aren't automatically available to outside code. Their default is to exist the same duration as any other local variable, available for garbage collection when the outer function is done executing.

To keep them around, you'll need to specify how you want to make them available. One option for that is to return them, adding a structure to hold them both:

var x = function () {
  return {
    add: function(a,b){return a + b},
    mul: function(a,b){return a * b},

Though, so far at least, the outer function isn't strictly necessary and could possibly be removed:

var x = {
  add: function(a,b){return a + b},
  mul: function(a,b){return a * b},

Figure 4 below shows how your Console should look after evaluating this expression. Type the following code into the Console. Try typing it out, character-by-character, rather than copy-pasting it.

See the comments inline below:

// You can set the function up as a "constructor function",
// meaning that an object instance will be constructed from the
// invocation of the function. Standard convention is to use
// PascalCase for constructor function names:
function X() {
  // Later, when an instance of this object is made,
  // members of the object can be gotten by identifying
  // them as going along with "this" object instance.
  // There is more to know about this (ie. properties are
  // created as I'm showing here, but methods are often 
  // added to the function's prototoype).
  this.add = function(a,b){return a + b};
  this.mul = function(a,b){return a * b};

// Use the function to construct an object instance
let instance = new X();

// Then, use that instance and its associated members.
// FYI: Don't use .innerHTML when you aren't getting/setting
// any HTML. Use .textContent for raw text.
document.getElementById("demo").textContent = "Add: " + instance.add(3,4);
document.getElementById("demo").textContent += " | Multiply: " + instance.mul(3,4);
<p>After a function has been stored in a variable,
the variable can be used as a function:</p>

<p id="demo">hello</p>

Instead, users need to manually load and run JavaScript functions through the Excel add-ins feature for the first time, and later it will get executed automatically every time the Excel file is opened on the same system.

What you are doing here is defining a function named x, and defining two variables inside that function, named add and mul. Due to the rules that JavaScript follows when it comes to item scope (Especially in the context of functions, see the section Function Scope), these variables are inaccessible outside of the function. To access them, you will need to have the function return an object defining them:

var x = function() {
  var add = function(a,b){return a + b};
  var mul = function(a,b){return a * b};

  return {
    add: add,
    mul: mul

These can now be accessed by calling the function, which will return these two values:

var y = x();
document.getElementById("demo").innerHTML = y.add(3,4);

JavaScript Execution - Tutorial to learn JavaScript Execution in simple, easy and step by step way with syntax, examples and notes. Covers topics like executing JavaScript inside head tag, in the body and through an external file. Also specifies some guidelines to write a JavaScript code.

In side function if you want to declare public variables to constuctor use this. And make sure to put new to create new instance of your constructor

var x = new function () {

  this.add = function(a,b){return a + b};
  this.mul = function(a,b){return a * b};


function addEventsAndStuff() { // bla bla } addEventsAndStuff(); function sendStuffToServer() { // send stuff // get HTML in response // replace DOM // add events: addEventsAndStuff(); } Re-adding the events is necessary because the DOM has changed, so previously attached events are gone.

The next approach you can take to run JavaScript is to either write some JavaScript directly into an HTML page or load a JavaScript file in to a web page that you are working on. This is especially great if you want to use your JavaScript code to update or generate the web page.

eval is evil in JavaScript! The MDN eval page states:. Obsolete This feature is obsolete. Although it is still supported by browsers, its usage is discouraged in new projects. Try to avoid using

Many Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make specific features on the web page functional. If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

  • change var add and var mul with this.add and this.mul
  • Why are you putting add and mul inside the function x? You could just get rid of x and have the add and mul functions separate, then just run add(3,4) or mul(3,4)
  • While your code may provide a valid solution , you should always provide a written explanation also explaining why it improves on OP's issue
  • This isn't a good way of solving this particular problem in this context, as the functions are no longer stored as plain variables.
  • Never use new function!
  • @Bergi can you please explain why?
  • @MaheerAli follow the link for a detailed explanation