javascript-today

Some more information about functions

Named parameters

when a function accepts more then 2 parameters its time to start using named parameters instead.

for example:

function somefunc(fName, lName, address, phone) {
}

//becomes

function somefunc({fname, lName,address, phone})

explination

when passing parameters to the function you don’t need to worry about the order in which they are passed. Also when looking at the function its easy to figure out what the parameters are.

Default parameters in a function

Sometimes you might want to be able to pass in a parameter but normally just want a default set.

function somefunc(address, phone, fName = "tom", lName = "smith") {}

// when parameters are passed without named parameters you have to be careful about order and optional parameters have to be last.

function somefunc({fName = "tom", lName = "smith", address, phone}) {}

explination

this means if you don’t pass those parameters they default to what is set.

Function Hoisting

Like var variables, function declarations are “hoisted” to the top of their scope. This means you can call a function before it’s declared in your code.