- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
在本教程中,您将借助示例了解 JavaScript 中的类型转换。
在编程中,类型转换是将一种类型的数据转换为另一种类型的过程。例如:将 String 数据转换为 Number。
JavaScript 中有两种类型的类型转换。
在某些情况下,JavaScript 会自动将一种数据类型转换为另一种正确的类型,这称为隐式转换。
// numeric string used with + gives string type
let result;
result = '3' + 2;
console.log(result) // "32"
result = '3' + true;
console.log(result); // "3true"
result = '3' + undefined;
console.log(result); // "3undefined"
result = '3' + null;
console.log(result); // "3null"
注意:当将数字添加到字符串时,JavaScript 会在连接之前将数字转换为字符串。
// numeric string used with - , / , * results number type
let result;
result = '4' - '2';
console.log(result); // 2
result = '4' - 2;
console.log(result); // 2
result = '4' * 2;
console.log(result); // 8
result = '4' / 2;
console.log(result); // 2
// non-numeric string used with - , / , * results to NaN
let result;
result = 'hello' - 'world';
console.log(result); // NaN
result = '4' - 'hello';
console.log(result); // NaN
// if boolean is used, true is 1, false is 0
let result;
result = '4' - true;
console.log(result); // 3
result = 4 + true;
console.log(result); // 5
result = 4 + false;
console.log(result); // 4
注意: JavaScript 将 0 视为 false,所有非零数视为 true。并且,如果 true 转换为数字,则结果始终为 1。
// null is 0 when used with number
let result;
result = 4 + null;
console.log(result); // 4
result = 4 - null;
console.log(result); // 4
// Arithmetic operation of undefined with number, boolean or null gives NaN
let result;
result = 4 + undefined;
console.log(result); // NaN
result = 4 - undefined;
console.log(result); // NaN
result = true + undefined;
console.log(result); // NaN
result = null + undefined;
console.log(result); // NaN
您还可以根据需要将一种数据类型转换为另一种数据类型。您手动执行的类型转换称为显式类型转换。
在 JavaScript 中,显式类型转换是使用内置方法完成的。
以下是一些常见的显式转换方法。
要将数字字符串和布尔值转换为数字,您可以使用 Number()。例如,
let result;
// string to number
result = Number('324');
console.log(result); // 324
result = Number('324e-1')
console.log(result); // 32.4
// boolean to number
result = Number(true);
console.log(result); // 1
result = Number(false);
console.log(result); // 0
在 JavaScript 中,空字符串和 null 值返回 0。例如,
let result;
result = Number(null);
console.log(result); // 0
let result = Number(' ')
console.log(result); // 0
如果字符串是无效数字,则结果将为NaN。例如,
let result;
result = Number('hello');
console.log(result); // NaN
result = Number(undefined);
console.log(result); // NaN
result = Number(NaN);
console.log(result); // NaN
注意:还可以使用 parseInt()、parseFloat()、一元运算符 + 和 Math.floor() 从字符串生成数字。例如,
let result;
result = parseInt('20.01');
console.log(result); // 20
result = parseFloat('20.01');
console.log(result); // 20.01
result = +'20.01';
console.log(result); // 20.01
result = Math.floor('20.01');
console.log(result); // 20
要将其他数据类型转换为字符串,您可以使用 String() 或 toString() 。例如,
//number to string
let result;
result = String(324);
console.log(result); // "324"
result = String(2 + 4);
console.log(result); // "6"
//other data types to string
result = String(null);
console.log(result); // "null"
result = String(undefined);
console.log(result); // "undefined"
result = String(NaN);
console.log(result); // "NaN"
result = String(true);
console.log(result); // "true"
result = String(false);
console.log(result); // "false"
// using toString()
result = (324).toString();
console.log(result); // "324"
result = true.toString();
console.log(result); // "true"
注意:String() 接受 null 和 undefined,并将它们转换为String。但是,当传递 null 时,toString() 会给出错误。
要将其他数据类型转换为布尔值,您可以使用 Boolean()。
在 JavaScript 中,undefined, null, 0, NaN, ’ ’ 转换为 false。 例如,
let result;
result = Boolean('');
console.log(result); // false
result = Boolean(0);
console.log(result); // false
result = Boolean(undefined);
console.log(result); // false
result = Boolean(null);
console.log(result); // false
result = Boolean(NaN);
console.log(result); // false
所有其他值给出 true。 例如,
result = Boolean(324);
console.log(result); // true
result = Boolean('hello');
console.log(result); // true
result = Boolean(' ');
console.log(result); // true
该表显示了 JavaScript 中不同值到字符串、数字和布尔值的转换。
值 | String 转换 | Number 转换 | Boolean 转换 |
---|---|---|---|
1 | “1” | 1 | true |
0 | “0” | 0 | false |
“1” | “1” | 1 | true |
“0” | “0” | 0 | true |
“ten” | “ten” | NaN | true |
true | “true” | 1 | true |
false | “false” | 0 | false |
null | “null” | 0 | false |
undefined | “undefined” | NaN | false |
‘’ | “” | 0 | false |
’ ’ | " " | 0 | true |
您将在后面的教程中了解对象和数组到其他数据类型的转换。
相关示例:
上一教程 :JS Comments 下一教程 :JS Comparison Operator
[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/type-conversion
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!