JavaScript default parameters


Since es6 its possible to include default function parameters just like other programing languages such as php do,
default parameters cut down the need to initially set default Parameters within the code as;

const vec function (x, y){

    if(typeof x == "undefined")

    x = 0;

if(typeof y == "undefined")

    y = 0;

    return {

        x, y

    }

}

now the above can be done in a more simple way , using default parameters as;


const vec function (x = 0, y = 0){
    return {

        x, y

    }

}
In either of the functions if you omit either of the Parameters your parameter will default to zero ie 0;

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript