Better functions with default parameters (javascript)
image author: https://www.google.com/imgres?imgurl=https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--_aWlnjbL--%2Fc_imagga_scale%2Cf_auto%2Cfl_progressive%2Ch_420%2Cq_auto%2Cw_1000%2Fhttps%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2

Better functions with default parameters (javascript)

Imagine the you have a function

function convertTemperature(celsius , decimalPlaces) {
    const fahrenheit = celsius * 1.8 + 32;
    return Number(fahrenheit.toFixed(decimalPlaces));
}

so that is a function that allow us to convert celsius to fahrenheit and handle de decimals of the result.

But what happen if you forget to put the decimal places values, our function will break, how can we avoid that kind of issues? the answer is simple, use default parameters:

function convertTemperature(celsius , decimalPlaces = 1) {
    const fahrenheit = celsius * 1.8 + 32;
    return Number(fahrenheit.toFixed(decimalPlaces));
}

What changed from the function above? when you defined the parameters you only have to add a default parameter with the "=" symbol and next to the value of the default parameter, example:

function anyfunction(param1 = defaultvalue){...}

if you read that, I hope to be of help to you <3


Alfredo Martínez García

Software Engineer || Front End Developer at Phase2Technology

4 å¹´

Thank you for Sharing bro!!

Vaibhav Mavani

Senior Full Stack Developer | JavaScript | React.js | Node.js | Angular.js | .NET | Java | Spring | Spring Boot | Python | C# | DSA | System Design & Architecture

4 å¹´

Thanks for sharing

要查看或添加评论,请登录

Bernardo Aguayo Ortega的更多文章

  • Get Subsets of arrays(4)

    Get Subsets of arrays(4)

    What if we only want a subset of data, no to transform the array we only want the elements that we are interesting on…

  • Perform Actions on all elements arrays(3)

    Perform Actions on all elements arrays(3)

    There are a method incredible powerful call map that allow us to make transform in each element of the array. //we have…

  • Check Element Existence in Arrays (2)

    Check Element Existence in Arrays (2)

    If you work with a hard code values is easy to work with them because you know where are the values inside the array…

  • Build Flexible Collections with Arrays (1)

    Build Flexible Collections with Arrays (1)

    There are a data structure to save collection of data call it array use [] brackets instead {}, Arrays are a kind of…

  • Merge Objects with Object Spread

    Merge Objects with Object Spread

    If you want to merge various objects you need to spread object's properties in new object and literary since ECMAScript…

  • Object Destructuring (JavaScript)

    Object Destructuring (JavaScript)

    Object destructuring allows us to pull properties from an object and make them into variables, in other words allow us…

  • Use Objects for Managing key-value pairs (js)

    Use Objects for Managing key-value pairs (js)

    We already know the different types of variables in JavaScript so you can see a variable as a box and an object like a…

    2 条评论
  • Shorter functions with arrow functions

    Shorter functions with arrow functions

    With ES6 we have a new form to write functions, that allow us to write shorter functions, we don't need any more the…

    4 条评论
  • How Functions Should Be Named

    How Functions Should Be Named

    A few things to remember when naming functions:Name functions after a verb which clearly conveys what they do Omit…

  • single responsibility functions

    single responsibility functions

    How to make your function only have one responsibility? As the name say it this principle talks about make functions…

社区洞察

其他会员也浏览了