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

What is 'this.' keyword in JavaScript

How to make a static http server in nodejs using express

How to get user's ip address in JavaScript

How to create promises in JavaScript

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