Forum Moderators: open

Message Too Old, No Replies

Default value sent to a function

         

csdude55

1:28 am on Mar 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been doing this since around '97 without any type of formal training, so I'm first to admit that some of my coding techniques could be old school at best, questionably wonky at worst :-( But I've always set a default value like so:

function foo(bar='whatever') {
console.log('bar = ' + bar);
}

foo('example');
# bar = example

foo()
# bar = whatever

Is this not considered "normal" anymore?

I was trying to minimize my code using jcompress.com, and it's throwing an error at the =:

Unexpected token: operator «=», expected: punc «,»

I can obviously modify the function to:

function foo(bar) {
if (!bar) bar = 'whatever';
console.log(bar);
}

// or, I guess
function foo(bar) {
bar = bar || 'whatever';
console.log(bar);
}

but it seems unnecessary... and I've used this format in a LOT of functions!

So who's wrong, jscompress.com or me? If it's me, has this always been wrong, or a new thing that I need to worry about?

Fotiman

4:33 am on Mar 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, I'm not sure when you started using default values like that, but that was something that was added as part of ES6, and is not supported in IE. But your code is not wrong. I've not used it before, but jscompress.com seems to have a checkbox just above the input that says "ECMAScript 2019 (via babel-minify)". You need that checked.

csdude55

4:36 pm on Mar 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure when I started doing that, but... I began using PHP over Perl sometime around 2008. I'm guessing that I saw it in PHP, tried it in Javascript, and it worked, so I've probably been doing it since 2008 or 09! But I'm pretty sure that I set most of the variables to false instead of 'whatever' so that I wouldn't have to check for "undefined".

Since Chrome was always the most strict, I really got in to the habit of just coding for Chrome and assuming that if it worked for the most strict then it would work everywhere. I've probably had a lot of IE errors that I never noticed because of that! :-O

csdude55

4:42 pm on Mar 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you know if it's an issue for jQuery, too? I'm not sure how to test it or to check caniuse.com for it...

Eg,

$.fn.foo = function(bar=false) { ... };

Fotiman

6:42 pm on Mar 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since jQuery is just a library that runs in JavaScript, and this is specifically a "syntax" change within JavaScript, the same would hold true. And to check caniuse.com:
https://caniuse.com/#feat=mdn-javascript_functions_default_parameters

csdude55

12:05 am on Mar 6, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahh, I see... well, good to know! Thanks :-)

NickMNS

1:13 pm on Mar 9, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure when I started doing that, but... I began using PHP over Perl sometime around 2008. I'm guessing that I saw it in PHP, tried it in Javascript, and it worked, so I've probably been doing it since 2008 or 09! But I'm pretty sure that I set most of the variables to false instead of 'whatever' so that I wouldn't have to check for "undefined".

I'm pretty sure that this "worked" before, but it didn't work the same as now. My guess is that assigning the variable within the function definition probably set a new global variable. Meaning that if you used x=false, then x would become a global variable, accessible from anywhere, not just from inside the function. Since "x" had a value assigned to, it would work as expected within the function, but if you used x again in your next function you might have been in for a surprise.

With the ES6 implementation, the variable is scoped only to the function where it is defined. Meaning that you could use the variable x in many functions without any issues.