1

I am reading a book and saw a function, but I could not understand the lines var numVal = +val; return val + '' === numVal + '';. Can anyone help explaining it a bit? Thanks in advance!

<!DOCTYPE html>
<html>
<body>
<script>

function isNumeric(val) {
    var numVal = +val;  // what's this for?
    return val + '' === numVal + ''; // what's this for?
}

function filterNumeric(arr) {
    var result = [];

  for(var i=0; i<arr.length; i++) {
    var val  = arr[i];
        if (isNumeric(val)) {
            result.push(val);
        }
  }

  return result;
}


var arr = ["a",1, 2, "b"];

arr = filterNumeric(arr);
alert(arr);


</script>

</body>
</html>
Dennisboys
  • 563
  • 3
  • 7
  • 21

4 Answers4

2

These are JavaScript tricks which essentially do type-casting (coercing).

  • The +val is the numeric unary + operator applied to val, which forces it to be interpreted as a number. If it helps, in English I always think of this as meaning "positive val" (not to be confused with absolute value). See also What's the significant use of unary plus and minus operators?.

  • The val + '' is the binary + operator applied to two objects val and the string ''. Namely, this is the string concatenation operator. This forces val to be interpreted as a string and then concatenated with the empty string (which does nothing). The same thing is done to numVal.

  • The === is the "strictly-equal" equality operator which tests both value and type. Note this is parsed as (val + '') === (numVal + ''). Since in this case you will always be comparing two strings, this is not really necessary; == would suffice.


The code is equivalent to the following psudocode:

//var numVal = +val
var numVal = numeric.Parse(val) or NaN;

//return val + '' === numVal + ''
return val.ToString().Equals(numVal.ToString());

Looking at some examples:

val     numVal   (val + '')   (numVal + '')   return
------------------------------------------------------
23      23       '23'         '23'            true
'23'    23       '23'         '23'            true
'23x'   NaN      '23x'        'NaN'           false
'junk'  NaN      'junk'       'NaN'           false
'1E23'  1e+23    '1E23'       '1e+23'         false
'1e+23' 1e+23    '1e+23'      '1e+23'         true
1e+23   1e+23    '1e+23'      '1e+23'         true

Community
  • 1
  • 1
lc.
  • 105,606
  • 20
  • 147
  • 176
1

The code actually is :

 return (val + '') === (numVal + '') ; 

which translates to A === B i.e. is A strictly equal to B , having same datatype and value. Here val and numVal are two integers ,by adding '' to them, they result gets typecasted into a string.

The statement compares two numeric strings, and returns true or false based on whether they are equal or not.

eg;

'1' === 1     // false
'1' == 1 // true
1 + ''   === '1'  // true
2 + '' ===  (1+1) + ''   // true
DhruvPathak
  • 38,316
  • 14
  • 103
  • 164
0

The + has a lot of meanings in Javascript. The first unary + works for numerical values. The second & third is catenating with the empty string to get a string representation of the number

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479
0
 var numVal = +val; 

When val is a string, numVal will be the string converted to a number. (similar to parseInt(), without the radix magic)

 val + '' === numVal + '';

This uses the identity operator === and checks if the string value(created by adding an empty string) of val and numval are the same. Basically, if numVal was NaN, it would evaluate to false. There really isn't any need for === here, == would have sufficed for the same operation.

Manishearth
  • 10,990
  • 5
  • 52
  • 73