Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yihan12 authored Dec 14, 2023
1 parent 8ed0478 commit 7bae4f3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions javascript/Type Casting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
强制类型转换是将值从一种数据类型自动或隐式地转换为另一种数据类型(例如字符串转换为数字)。类型转换类似于强制类型转换,因为它们都将值从一种数据类型转换为另一种数据类型,只有一个关键的区别——强制类型转换是隐式的,而类型转换可以是隐式的,也可以是显式的。

```javascript
const value1 = "5";
const value2 = 9;
let sum = value1 + value2;
console.log(String(2 - true)); // '1'
console.log('56' === String(56)); // true

console.log(sum); // '59'
console.log(Number(prompt())); // converts prompt value into a Number
console.log(Number('2350e-2')); // '23.5'
console.log(Number('23') + 7); // 30

console.log(Boolean('')); // false
console.log(Boolean(2) == true); //true
```

### 隐式类型转换

隐式类型转换是将一种数据类型转换为另一种数据类型(确保在相同的数据类型之间完成操作),以便运算符或函数正常工作。

```javascript
// 隐式字符串转换
console.log('25' + 15); // '2515'

// 隐式Number转换
console.log(23 * '2'); // 46
console.log(23 - true); // 22 // true is converted into 1
console.log(true - null); // 1
console.log(false + undefined); // NaN // undefined into NaN

const arr = [];
if(arr) { console.log('Hello World') }; // 'Hello World'
```

0 comments on commit 7bae4f3

Please sign in to comment.