Parameters of function in Javascript

javascript_logo_unofficial-300x300

Currently, Javascript language is not strangers for programmers . Today , I would like to introduce management parameters passed to a function in the javascript language . Unlike languages ​​such as C , C + + , Java … then the variable declaration should be cleared and the parameter passed to a function is fixed , if you want to pass the parameters more or less to the function defined before that it is impossible ( just as you can use methology like overloading in function) . So what is the difference ?

Javascript manage the parameters passed to the function through an array named arguments this is  a global array.

For example :

I declare a function as follows

function reciveArgument( ) {

/ / this function will return parameters that you passed

return arguments ;

}

and when I call this function with different parameters as following :

console.log ( reciveArgument ( ” Parameter 1 ” , ” Parameter 2 ” , ” Parameter 3 ” ) ) ;

when you open up your console in your browser and see

>>>[“Parameter 1”, “Parameter 2”, “Parameter 3”]

Try passing by numbers

console.log ( reciveArgument ( 1,2,3 ) ) ;

The  results returned as following:

>>[1, 2, 3]

So the arguments array is available in the function we do not need to care about  overload function . For example, you write function sum ( ) of natural numbers , then just do a shoulder operation as follows :

function sum( ) {

var total = 0 ;

for ( var i in arguments) {
total + = arguments [ i ] ;
}
return total;

}

and you call this function again with different parameters

console.log ( sum ( 1,2,3 ) ) ;

console.log ( sum ( 1,2,3,4,5 ) ) ;

The  results returned as following:

6

15

When you research the JavaScript language , you will find  more interesting things . Thank you

Blog Lastest

We are providing high skilled and quality web based and open sourced system development service at our Vietnamese offshore centre.
At the same centre, there are also various services for software/system maintenance, software product development and web

On the occasion of the 75th anniversary of the successful August Revolution and the National Day of the Socialist Republic of Vietnam on September 2, 1945 – September 2, 2020, gumi Vietnam wish that everyone have good health and be happy with family. Besides, gumi Viet Nam would like to announce the company’s holiday schedule...

Hello this is first time i try to make a small game by Libgdx, before i make this game i plan/design contruct for this game, but when i research and write by Libgdx, i think everything don’t like i throught. LibGDX don’t have any layout, all object, image dont appear. I just see text..text…and text,...

Today, the web gaming applications become increasingly popular and diverse … these apps are created from many different programming languages ​​. Today I would like to introduce a game demo written by three language are: html, css and javascript language and I already had one game name is BoomBoomBalloon at: BoomBoomBalloon demo you can try...