How to multiply two numbers with javascript function?

Javascript function is often used to carry out multiple tasks ranging from showing alert box to changing the inner html value of a webpage. Below code will create a function which will multiply two numbers passed into it and showing the result trough the alert box!

multiply(3, 4);
function multiply(a, b)
{
var factor = a * b;
alert(factor);
}

In order to use the above javascript multiply function all we need to do is to pass two numbers into that function and it will carry out it’s job accordingly.

Javascript call method tutorial

Javascript’s call method looks really complicated but actually it is not as difficult as you think it is. Let takes a look at one example of how to use the javascript’s call method to show an alert box with greeting message!

Below example will show a greeting message after the web page has been loaded.

$(document).ready(function() {

function hello() {
  var hello = ['Good morning ', this.name, this.sayHello].join(' ');
  alert(hello);
}

var obj = {
  name: 'MR Noob', 
  sayHello: ', how are you today?'
};

hello.call(obj); 

});

The call method above takes one argument, which is the object that will be used in the alert method in hello function. As you can see the keyword this in the hello function will be replaced by the object after the call. That is basically what will happen when we passed an object into a call method of another object.

Let takes a look at another example, this time we will call the Computer function within the Laptop function and pass in the brand and price to the call method of the Computer function. What the below does is to show an alert box with the values we have passed in as well as using the origin property of the Laptop computer in the alert box.

jQuery(document).ready(function() {

	function Computer(brand, price) {
		
		var product_brand_price = ['I bought ', brand, ' which is made in ', this.origin, ' with $', price, '!'].join(' ');
		alert(product_brand_price);

	}

	function Laptop(brand, price) {
		this.origin = 'USA';
		Computer.call(this, brand, price);
	}

	var myPc = new Laptop("Dell", 3000);
});
Javascript call method
Javascript call method

There is one thing you need to take note of which is if you use the this keyword to represent the Computer object within the Computer object then this will no longer refer to the Laptop object anymore and thus if you call this.origin within that alert box then it will return an empty value because this will now represent the Computer object instead of the Laptop object and the Computer object does not have the origin property.