14

I'm new into the javascript world and I have not found any information about this notation. I found it in that topic (see the answer): Convert HH:MM:SS string to seconds only in javascript.

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

Also I wanted to use that code to convert 'HH:MM:SS' string to seconds. But, it seems unsafe for me. If the user inserts 'XX:03:SS', the value will be NaN which is not correct (at least for me). So I decided to improved it with:

function convertHHMMSSToSeconds(time) {

// initialize seconds
var seconds = 0;

//ensure time
if (!time) {
    return seconds;
}


try {
    var hmsTab = time.split(':'); // split it at the colons

    // ensure that the hmsTab contains 3 values (hh,mm,ss)
    if (!hmsTab || hmsTab.length !== 3) {
        return seconds;
    }

    // initialize hh, mm and ss
    var hh = hmsTab[0] > 0 && hmsTab[0] < 60? +hmsTab[0] : 0;
    var mm =  hmsTab[1] > 0 && hmsTab[1] < 60 ? +hmsTab[1] : 0;
    var ss =  hmsTab[2] > 0 && hmsTab[2] < 60 ? +hmsTab[2] : 0;

    // return 0 if one variable is not valid 
    if(+hmsTab[0] !== hh ||+hmsTab[1] !== mm || +hmsTab[2] !== ss) {
        return seconds;
    }

    // minutes are worth 60 seconds. Hours are worth 60 minutes.
    seconds = (hh * 60 * 60) + (mm * 60) + ss;
}catch (error)
{
    seconds = 0;
}
return seconds && seconds>0 ? seconds : 0;

}

So my question still remains, what does (+var) mean.

Regards,

Community
  • 1
  • 1
BironDavid
  • 503
  • 5
  • 19

4 Answers4

30

The + sign in front of a variable, will cast that variable to a number. Example:

var x = "3";
var y = x + 10; // 310
var z = +x + 10 // 13
DJ22T
  • 1,628
  • 3
  • 34
  • 66
tymeJV
  • 103,943
  • 14
  • 161
  • 157
11

+var is the application of the unary identity operator to the value of var. The identity operator can be thought of as similar to a function that returns its only parameter:

function identity(operand) {
    return operand;
}

However, the identity operator only acts on numeric values. Since Javascript is a weak-typed language, applying a numeric function to a non-numeric value will cause the non-numeric value to be coerced into an equivalent numeric value. In the most common case for coercing using the identity operator, a string containing a number is parsed into a number.

While quick to type and easy to remember, coercion with the identity operator is not very robust. Here are some sample outputs:

+'150'  // 150
+'150z' // NaN
+'z150' // NaN
+'015'  // 15 -- note that it doesn't interpret as octal*
+'015z' // NaN
+'z015' // NaN
+'0xf'  // 15 -- note that it interprets as hexadecimal
+'0xfz' // NaN
+'z0xf' // NaN
+'NaN'  // NaN
+'undefined' // NaN

Compare the same inputs with parseInt:

parseInt('150')   // 150
parseInt('150z')  // 150
parseInt('z150')  // NaN
parseInt('015')   // 15 -- note that it still doesn't interpret as octal*
parseInt('015z')  // 15
parseInt('z015')  // NaN
parseInt('0xf')   // 15 -- note that it still interprets as hexadecimal
parseInt('0xfz')  // 15
parseInt('z0xf')  // NaN
parseInt('NaN')   // NaN
parseInt('undefined') // NaN

parseInt also gives you more control over the result:

parseInt('015', 8)   // 13
parseInt('z0xf', 36) // 1634163
parseInt('1010', 2)  // 10

* In EMCAScript 5. Previous versions would default to octal when dealing with leading zeroes.

Strings are not the only thing which can be coerced into numbers with the identity operator, although they are the most common, and the conversion is the most sensible. Other coercions might make sense, or they might not. Example:

+[]        // 0
+[150]     // 150
+['150']   // 150
+['150z']  // NaN
+[1, 2]    // NaN
+{}        // NaN
+null      // 0
+true      // 1
+false     // 0
+NaN       // NaN
+undefined // NaN
+function(){} // NaN
Brian S
  • 4,878
  • 4
  • 27
  • 46
6

A way of convertion to number:

+n === Number(n)
Engineer
  • 47,849
  • 12
  • 88
  • 91
5

Its shortest form to convert a variable to number

Satpal
  • 132,252
  • 13
  • 159
  • 168