What is the meaning of an Underscore in javascript function parameter?

后端 未结 2 1548
情书的邮戳
情书的邮戳 2020-12-05 00:07

I was going through the code of one of the chart library written in javascript, wherein I\'ve seen passing underscore(_) as a function parameter. What does that mean?

<
2条回答
  •  生来不讨喜
    2020-12-05 00:43

    _ in fat arrow function is called as throwaway variable. It means that actually we're creating an variable but simply ignoring it. More devs are now a days using this as syntactic sugar or short hand while writing code, as it's easy and one character less to write the code.

    Instead of using _, you can use other variables like temp, x, etc

    for examples:

    () => console.log('Hello World')
    
    
    
    _ => console.log('Hello World')
    
    
    
    
    x => console.log('Hello World')
    

    But personally i prefer to use () type over throwaway variable if no arguments are needed.

    See the following code, then you will understand it better.

    _ as an argument,

      f = _=> {
        return _ + 2 ;
    }
    

    f(3) will return 5

    For better understanding, check wes bos

提交回复
热议问题