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 set content disposition header for nginx server to force content download

Animated daylight on HTML5 Canvas

How to make a static http server in nodejs using express

JS13K Preparation

Adding Gameover and introduction screen on HTML Canvas