Can someone explain to me what the plus sign is before the variables in JavaScript?

The plus (+) sign before a variable in JavaScript is the unary plus operator. It converts the value to a number, making it useful for type conversion from strings, booleans, or other data types to numeric values.

Syntax

+value

How It Works

The unary plus operator attempts to convert its operand to a number. It works similarly to Number() constructor but with shorter syntax.

Example: String to Number Conversion

var firstValue = "1000";
console.log("The data type of firstValue = " + typeof firstValue);

var secondValue = 1000;
console.log("The data type of secondValue = " + typeof secondValue);

console.log("The data type of firstValue when + sign is used = " + typeof +firstValue);

var output = +firstValue + secondValue;
console.log("Addition is = " + output);
The data type of firstValue = string
The data type of secondValue = number
The data type of firstValue when + sign is used = number
Addition is = 2000

Common Use Cases

// Converting strings to numbers
console.log(+"42");        // 42
console.log(+"3.14");      // 3.14
console.log(+"");          // 0

// Converting booleans to numbers
console.log(+true);        // 1
console.log(+false);       // 0

// Converting null and undefined
console.log(+null);        // 0
console.log(+undefined);   // NaN
42
3.14
0
1
0
0
NaN

Comparison with Other Methods

Method Syntax Result for "123" Result for "abc"
Unary Plus +value 123 NaN
Number() Number(value) 123 NaN
parseInt() parseInt(value) 123 NaN

Key Points

  • Returns NaN for non-numeric strings
  • Converts empty string to 0
  • Faster than Number() constructor
  • Commonly used in mathematical operations

Conclusion

The unary plus operator (+) is a concise way to convert values to numbers in JavaScript. It's particularly useful for ensuring numeric operations work correctly with string inputs.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements