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

FlappingBird Postmortem (JS13K)

JavaScript introduction to variables

FlappingBird JS13K Games

How to target all elements of a given class name in JavaScript

Zedplug will be decommissioned on November 30th