JavaScript类型转换

Some common data types in JavaScript

JavaScript中的一些常见数据类型

Number1, 2, 3, 400, -1433, etc
String'hello world', 'include help'
BooleanTrue/False
NullExplicitly sets a variable with no value
UndefinedFor variables that have not been defined yet
ObjectComplex data structures like arrays, dates, literals, etc
SymbolUsed with objects
1,2,3,400,-1433等
“ hello world”,“包括帮助”
布尔型 真假
空值 明确设置没有值的变量
未定义 对于尚未定义的变量
目的 复杂的数据结构,例如数组,日期,文字等
符号 与对象一起使用

类型转换 (Type Conversion)

Type Conversion is the method by which we convert one data type into another. Type conversion is often used when creating applications with Javascript. We'll look at some basic methods to convert one type of data to another.

类型转换是一种将一种数据类型转换为另一种数据类型的方法。 当使用Javascript创建应用程序时,通常会使用类型转换。 我们将介绍一些将一种数据类型转换为另一种数据的基本方法。

Open the chrome dev console to try out the examples by right-clicking on the browserselecting inspectselecting console or simply press f12.

右键单击浏览器选择检查选择控制台,打开chrome dev控制台以尝试示例,或者直接按f12键

Let's say you create a string score and assigned a value 100 to it. Now you want to increment its value by one. If you simply do score+1 it will take 1 and concatenate to the string.

假设您创建了一个字符串乐谱并为其分配了值100。 现在,您要将其值增加一。 如果您只执行score + 1 ,它将取1并连接到字符串。

Code:

码:

    let score = '100';
    score +1 

Output:

输出:

    "1001"

type conversion in JS | 1

To do what we're trying to do, we should logically convert score into a number and then increment its value. We do this by assigning the new typecast value to our variable bypassing that variable as a parameter to our type conversion method.

要执行我们想做的事情,我们应该在逻辑上将分数转换为数字 ,然后增加其值。 为此,我们将新的类型转换值分配给变量,而绕过该变量作为类型转换方法的参数。

    > score = Number(score)
    < 100
    > score +1 
    < 101

type conversion in JS | 2

You can check the type of any variable using the typeof() method. Here we converted score which was originally a string to a number. However, if you try to convert a string to a number that logically shouldn't be a number at all, you will get NaN which stands for a Not a Number.

您可以使用typeof()方法检查任何变量的类型。 在这里,我们将原来是字符串的 score转换为数字 。 但是,如果您尝试将字符串转换为一个数字 ,逻辑不应该在所有的数字,你会得到NaN的这代表了一个不是数字

type conversion in JS | 3

It doesn't make sense to store 'hello' as a number. JavaScript is built this way. It first evaluates how the data behaves as another data type and won't typecast the value if it's typed after typecasting is logically incorrect. That's why it gives us 'NaN' or "Not a Number. We can also convert a number into a string the same way.

“ hello”存储为数字没有任何意义。 JavaScript是通过这种方式构建的。 它首先评估数据作为另一种数据类型的行为,如果在类型转换逻辑上不正确后再键入该值,则不会对该类型进行值转换。 这就是为什么它给我们'NaN'“ Not Number”的原因 。我们也可以用相同的方式将数字转换为字符串。

type conversion in JS | 4

Let's look at another example for type conversion: We declare a number and try to convert it to a boolean.

让我们看一下类型转换的另一个示例 :我们声明一个数字,然后尝试将其转换为布尔值 。

type conversion in JS | 5

In JavaScript, everything except 0 is considered true. That's why when we converted 50, a value other than 0, we got truthy and on passing 0 we got a false.

在JavaScript中,除0外的所有内容都视为true 。 这就是为什么当我们转换50 (不是0的值)时会得到真实的结果,而在传递0时会得到false的原因

All these type conversion methods are explicit since we, the programmer are carrying it out. Sometimes to evaluate certain expressions, JavaScript implicitly carries out type casting which is called Implicit Type Conversion.

所有这些类型转换方法都是显式的,因为程序员正在执行它。 有时,为了评估某些表达式,JavaScript会隐式执行类型转换 ,这称为隐式类型转换

翻译自: https://www.includehelp.com/code-snippets/javascript-type-conversions.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值