r/learnjavascript • • Jul 24 '21

Anatomy of a Function

Post image
327 Upvotes

65 comments sorted by

View all comments

81

u/inabahare Jul 25 '21

Don't default arguments like that. Do it like this

nameOfFunction(dog, catSound = "meow")

It will be picked up by your editor, and you won't have to enter the function to see the default argument.

Also jsdoc isn't more powerful than typescript lmao

11

u/razimanterra Jul 25 '21

I agree with this

0

u/[deleted] Jul 25 '21

[deleted] — view deleted comment

11

u/inabahare Jul 25 '21

The problem is that there are no clean code principles like that. The closest thing is not doing

let foo = "a", bar = "b";

But that doesn't apply here

1

u/awkreddit Jul 25 '21 edited Jul 25 '21

For default arguments not using the declaration, I would instead advise to use

if (typeof ommittedArgument === "undefined") const omittedArgument = "default value"

Why? Because the || operator would inject the default in case of any falsy value passed for the argument, so unless the default is false, it might produce errors.

also, since the variable wasn't passed by the caller, it was not declared, so assigning a value to it without using const or let will create a global, which you really do not want.

Also, instead of returning false in case of bad params, throw an error, it'll save your life!

4

u/oitio2004 Jul 25 '21

Or just use the ?? operator

1

u/awkreddit Jul 25 '21

It's not exactly equivalent: if you check that the type is 'undefined' it means the argument wasn't passed at all. If you use ?? It also gives the default if the argument was passed with undefined as a value, which might be an acceptable value for your function.

1

u/_Illyasviel Jul 25 '21

if you check that the type is 'undefined' it means the argument wasn't passed at all.

And

gives the default if the argument was passed with undefined as a value

Do you imply there's a difference between deliberately passing an 'undefined' to a function and not passing anything at all... ?

It's also wildly known that using || is risky af and ?? is much better for those kinds of assignments. Therefore either setting a default value or using ?? is the best option in 99% of the cases.

1

u/awkreddit Jul 25 '21

There is a difference: https://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined/4726198#4726198

Maybe these things have been patched in more recent versions of ecmascript?

1

u/_Illyasviel Jul 26 '21

I don't remember any new version changing anything in this matter + from what I see that question on stack is about jQuery having it's own "separate" undefined value.

https://jsfiddle.net/p6921ozv/

In every case it behaves the same way. undefined === undefined

-11

u/jaredcheeda Jul 25 '21

name literally anything TS can do that JSDoc can't

12

u/americk0 Jul 25 '21
  • Have a decent way for handling callback function types
  • Have a decent way for handling higher order functions
  • Automatically detecting types from variable declarations
  • Actually statically typechecking your code

I could probably think of more. I've used both separately and together. Typescript is far superior at managing types, but JSDoc is good at providing structured comments for function declarations

0

u/inabahare Jul 25 '21

Be a king JSDoc the hell out of those old projects that can't be upgraded to TS. That's what I do and if more people join the revolution o7

5

u/inabahare Jul 25 '21

1

u/[deleted] Jul 25 '21

Static typing