Unary Operator: A unary operation contain only one operand. Here, ‘+’ unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation.
JavaScript Identifiers: Javascript Identifiers are used to name variables (and keywords, and functions, and labels).In Javascript, the first character must be a letter, or an underscore ( _ ), or a dollar sign ( $ ) but not digit and subsequent characters may be letters, digits, underscores, or dollar signs.
JavaScript +_ operator: It is a combination of the variable with symbol underscore( _ ) and unary plus ( + ) operator, + operator converts the type of _ variable to Number type for further operation.
Example: The variable “_” with string type is stored in variable “r” with “number” type.
- Input:
var _ = "1"; var r = +_;
- Output:
typeof(r) will be number
Example 1: Conversion of String to Number
HTML
<!DOCTYPE html> <html> <head> <title>Javascript Operator</title> <script type="text/javascript"></script> <link rel="stylesheet" </head> <body> <div class="conintainer"> <p class="w3-jumbo w3-text-green pad" align="center" style="margin: 0 0 0 0;"> GeeksforGeeks </p> <p class="w3-large w3-text-green pad" align="center"> A computer science portal for geeks </p> <p align="center"> Type of variable(_) : <span id="gfg"></span> <br /> Type of variable(a) : <span id="gfg1"></span> </p> <script type="text/javascript"> GFG = function (_) { let b = typeof _; let a = +_; let c = typeof a; document.getElementById("gfg").innerHTML = b; document.getElementById("gfg1").innerHTML = c; }; GFG("21"); </script> </div> </body> </html> |
Output:
Example 2: Performing arithmetic addition operation by converting them to number type.
HTML
<!DOCTYPE html> <html> <head> <title>Addition Operation</title> <script type="text/javascript"></script> <link rel="stylesheet" </head> <body> <div class="conintainer"> <p class="w3-jumbo w3-text-green pad" align="center" style="margin: 0 0 0 0;"> GeeksforGeeks </p> <p class="w3-large w3-text-green pad" align="center"> A computer science portal for geeks </p> <p align="center"> Value variable(_) : <span id="gfg"></span> <br /> Value of variable($) : <span id="gfg1"></span> <br /> Value of variable(Sum) : <span id="gfg2"></span> </p> <script type="text/javascript"> GFG = function (_, $) { let c = +_ + +$; let a = +_; let b = +$; document.getElementById("gfg").innerHTML = a; document.getElementById("gfg1").innerHTML = b; document.getElementById("gfg2").innerHTML = c; }; GFG("21", "45"); </script> </div> </body> </html> |
Output:
Recommended Posts:
- What does javascript:void(0) mean?
- What does enumerable property mean in JavaScript?
- What does the â+â (plus sign) CSS selector mean?
- What does '<?=' short open tag mean in PHP ?
- What does it mean to start a PHP function with an ampersand?
- What is the difference between MEAN.js and MEAN.io?
- Ternary operator vs Null coalescing operator in PHP
- JavaScript Course | Conditional Operator in JavaScript
- How does internationalization work in JavaScript?
- How does inline JavaScript work with HTML ?
- What does the leading semicolon in JavaScript libraries do ?
- JavaScript | â===â vs â==âComparison Operator
- JavaScript typeof operator
- Difference between == and === operator in JavaScript
- Arrow operator in ES6 of JavaScript
- JavaScript | Spread Operator
- What is the !! (not not) operator in JavaScript?
- JavaScript Instanceof Operator
- JavaScript Ternary Operator
- How to get negative result using modulo operator in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


