js call function code example

Example 1: call js

function Product(name, price) {
  this.name = name;
  this.price = price;

  if (price < 0)
    throw RangeError('Cannot create product "' + name + '" with a negative price');
  return this;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}
Food.prototype = new Product();

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = 'toy';
}
Toy.prototype = new Product();

var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);

Example 2: running a function in a function javascript

function runFunction() {
  myFunction();
}

function myFunction() {
  alert("runFunction made me run");
}

runFunction();

Example 3: javascript function call with variable

function abc() {
  alert('test');
}

var funcName = 'abc';

window[funcName]();

Example 4: Functions call functions js

/*
Write a function sum that takes an array of numbers and 
returns the sum of these numbers. 
Write a function mean that takes an array of numbers and 
returns the average of these numbers. 
The mean function should use the sum function.
*/
function sum (arr) {
    let suma = 0;
    for (let i = 0; i < arr.length; i++) {
        
        let num= parseInt(arr[i]);
        suma += num;
    }
    return suma;
  }
  
  function mean (arr) {
    let average = sum(arr) /arr.length;
    
    return average;
  }

Example 5: javscript call

myFunc.call(thisArg, ...args)

Example 6: call function javascript

// Define your function
function sayHello(msg){
	console.log("Hello, " + msg);
}

// Call it
sayHello("Norris");

// outputs:
// Hello, Norris