Make your JavaScript life easier by applying these methods to your Functions

When calling JavaScript functions, you pretty much always need to provide arguments. But these turn into a mess pretty quickly if you’re not careful. So I’ve listed a few ways for you to make your life as a JavaScript developer easier, make your code easier to read and let your co-workers read your work faster too!

Just imagine a basic function with 3 arguments that does some magic with your data and returns a result. Pretty straightforward right? I’m sure you can find a function in your code that fits that description. I’m going with a fictional function that just does something silly.

function myFunction(a, b, c) {
    const result = a*b;
    const code = `${c}-${result}`;
    return code;
}

const answer = myFunction(2,3,'A');
console.log(answer);

This will output A-6 , but you can’t know that by reading the arguments. You could perhaps interpret that by changing the function name, but that only improves readability only so much.

  • Proper Function and Variable Names
  • Named Arguments
  • Default Values
  • Cut up long functions
  • Return multiple variables

#javascript

5 Ways to Improve Your Functions in JavaScript
2.15 GEEK