Sometimes I see two different ways to define default value for function arguments.
The first one is to redefine argument value like that:
var callName1 = function( name ) {
name = name || 'John';
console.log( 'Hello, ' + name );
};
The second one is to define local variable with the same name:
var callName2 = function( name ) {
var name = name || 'John';
console.log( 'Hello, ' + name );
}
Both of this methods are working the same.
So, I have two questions:
1) What's the point to define local variable with same name in the second way?
2) Which of these ways is more correct?
nameis already defined in the function and there is no need for a redefinition. On the other hand in ES6 you can avoid both usages by the default argument values such asvar callName1 = function(name = "John") {...