+ _运算符在JavaScript中是什么意思?

2021年3月11日17:18:51 发表评论 671 次浏览

本文概述

一元运算符:一元运算仅包含一个操作数。在此, " +"一元加运算符将其操作数转换为Number类型。同时它还充当具有两个操作数的算术运算符, 在计算时返回加法结果。

JavaScript标识符:Javascript标识符用于命名变量(以及关键字, 函数和标签)。在Javascript中, 第一个字符必须是字母, 下划线(_)或美元符号($), 但不能为数字, 后续的字符可以是字母, 数字, 下划线或美元符号。

JavaScript + _运算子:它是带符号下划线(_)的变量和一元加号(+)运算符的组合, +运算符将_变量的类型转换为Number类型以进行进一步的操作。

例子:字符串类型的变量" _"存储在数字类型的变量" r"中。

  • 输入如下:
    var _ = "1";
    var r = +_;
  • 输出如下:
    typeof(r) will be number

范例1:字符串到数字的转换

的HTML

<!DOCTYPE html>
< html >
     < head >
         < title >Javascript Operator</ title >
         < script type = "text/javascript" ></ script >
         < link rel = "stylesheet" 
               href = "https://www.lsbin.com/w3css/4/w3.css" />
     </ head >
     < body >
         < div class = "conintainer" >
             < p class = "w3-jumbo w3-text-green pad" 
                align = "center" 
                style = "margin: 0 0 0 0;" >
               lsbin
             </ 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 >

输出如下:

+ _运算符在JavaScript中是什么意思?1

范例2:通过将它们转换为数字类型来执行算术加法运算。

的HTML

<!DOCTYPE html>
< html >
     < head >
         < title >Addition Operation</ title >
         < script type = "text/javascript" ></ script >
         < link rel = "stylesheet" 
               href = "https://www.lsbin.com/w3css/4/w3.css" />
     </ head >
     < body >
         < div class = "conintainer" >
             < p class = "w3-jumbo w3-text-green pad" 
                align = "center"
                style = "margin: 0 0 0 0;" >
               lsbin
             </ 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 >

输出如下:

+ _运算符在JavaScript中是什么意思?2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: