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

Vietnam’s National Day 2/9 is coming, gumi Vietnam would like to inform you about the schedule of Vietnam’s National Day 2023 as following: We will be closed to observe National Day 2023 from 1/9/2023 to 4/9/2023 Business operation will resume as normal on 5/9/2023 Many thanks for your kind attention and being appreciative with this...

On July 1st, gumi held a KICK-OFF MEETING to be prepared for the new financial year. The meeting took place in a warm space with the presence of all company employees. Especially, Kick-off 2022 also means an event marking the 10-year growth journey of gumi Vietnam. Over the past 10 years, GUMI Vietnam has gradually...

Dear: Customers & Partners, In order to celebrate the National Day of September 2, gumi Vietnam would like to inform our customers that the company’s holiday schedule is as follows: – Time off: Thursday, September 1st, 2022 and Friday, September 2nd, 2022 – Re-working time: September 5th, 2022 During the holiday period, business operations will...

#Job requirements:  – Manage information technology and computer systems, ensure security data in company internal. – Plan, organize, control, and evaluate IT and electronic data operations – Design, develop, implement, and coordinate systems, policies, and IN – OUT procedures – Ensure security of data, network access, and backup systems – Act in alignment with user...