Khi bạn đang lập trình, bạn sẽ thường xuyên cần chuyển đổi giữa các loại dữ liệu. Ví dụ: bạn có thể cần chuyển đổi một chuỗi thành một số
Khả năng chuyển đổi loại dữ liệu này sang loại dữ liệu khác giúp bạn linh hoạt hơn khi làm việc với thông tin
JavaScript có các phương thức tích hợp khác nhau để chuyển đổi hoặc truyền chuỗi thành số
Trong bài viết này, bạn sẽ tìm hiểu một số phương thức JavaScript tích hợp có sẵn để chuyển đổi chuỗi thành số, cùng với phần giới thiệu [hoặc bồi dưỡng. ] đến kiến thức cơ bản về cách chuỗi và số hoạt động trong JavaScript
Đây là những gì chúng tôi sẽ đề cập
- Chuỗi trong JavaScript là gì?
- Một số trong JavaScript là gì?
- Làm cách nào để kiểm tra kiểu dữ liệu của một giá trị trong JavaScript?
- Cách chuyển đổi chuỗi thành số bằng hàm
4// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
- Cách chuyển chuỗi thành số bằng hàm
5// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
- Cách chuyển chuỗi thành số bằng hàm
6// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
- Cách chuyển đổi chuỗi thành số bằng hàm
7// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
- Cách chuyển đổi một chuỗi thành một số bằng cách nhân và chia cho ________ 08
- Cách chuyển đổi một chuỗi thành một số bằng cách sử dụng toán tử
9 đơn nguyên// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
Chuỗi trong JavaScript là gì?
Chuỗi là một cách giao tiếp hiệu quả thông qua văn bản, chẳng hạn như lưu trữ và thao tác văn bản. Chúng là một trong những kiểu dữ liệu cơ bản nhất trong tất cả các ngôn ngữ lập trình
Chuỗi trong JavaScript là một kiểu dữ liệu nguyên thủy. Điều này có nghĩa là chúng được tích hợp vào ngôn ngữ theo mặc định
Một chuỗi là một chuỗi có thứ tự gồm 0 hoặc nhiều giá trị ký tự. Cụ thể, đó là một chuỗi gồm một hoặc nhiều ký tự có thể là chữ cái, số hoặc ký hiệu [chẳng hạn như dấu chấm câu]
Nói chung, bạn có thể biết liệu một giá trị dữ liệu có phải là một chuỗi hay không nếu nó được đặt trong dấu ngoặc kép, chẳng hạn như dấu ngoặc đơn hoặc dấu ngoặc kép
Cụ thể, có ba cách bạn có thể tạo một chuỗi trong JavaScript
- Bằng cách sử dụng dấu nháy đơn
- Bằng cách sử dụng dấu ngoặc kép
- Bằng cách sử dụng backticks
Đây là cách tạo một chuỗi bằng dấu nháy đơn
// string created using single quotes ['']
let favePhrase = 'Hello World!';
Đây là cách tạo một chuỗi bằng cách sử dụng dấu ngoặc kép
// string created using double quotes [""]
let favePhrase = "Hello World!";
Và đây là cách tạo chuỗi bằng backticks
________số 8Cách cuối cùng để tạo chuỗi trong JavaScript còn được gọi là mẫu chữ
Số trong JavaScript là gì?
Các số cho phép bạn biểu diễn các giá trị số và thực hiện các phép toán và phép tính
Các số trong JavaScript là một kiểu dữ liệu nguyên thủy – giống như các chuỗi
Không giống như các ngôn ngữ lập trình khác, bạn không cần chỉ định loại số bạn muốn tạo. Ví dụ: bạn không cần đề cập đến số sẽ là số nguyên hay số float
Trong JavaScript, có một số loại số khác nhau [cả dương và âm] được tích hợp trong ngôn ngữ
- số nguyên. Số nguyên là một giá trị số không bao gồm phần thập phân - hay còn gọi là số tròn chục hay số nguyên
- phao nổi. Số float là một số có phần thập phân và ít nhất một số sau dấu thập phân
- Số mũ là những số có thể là số nguyên hoặc số thực và được theo sau bởi một số
0.// this is a string not a number! let num = '7';
0 biểu thị phép nhân một số với// this is a string not a number! let num = '7';
2 được nâng lên một lũy thừa cho trước// this is a string not a number! let num = '7';
- Số nhị phân [còn được gọi là cơ số 2]. Nhị phân là một hệ thống số bao gồm chỉ có hai số.
3 và// this is a string not a number! let num = '7';
8. Nó sử dụng 8 bit để biểu diễn một byte. Số bắt đầu bằng// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
3 theo sau là// this is a string not a number! let num = '7';
6 theo sau là số 8 bit// this is a string not a number! let num = '7';
- Số bát phân [còn được gọi là cơ số 8]. Một số bát phân bắt đầu bằng một
3 theo sau là các chữ số bát phân nằm trong khoảng từ// this is a string not a number! let num = '7';
8// this is a string not a number! let num = '7';
- Số thập lục phân [còn được gọi là cơ số 16]. Một số thập lục phân bắt đầu bằng một
3 theo sau bởi một trong hai// this is a string not a number! let num = '7';
90 hoặc// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
91. Sau đó, có thể có sự kết hợp của các chữ số thập lục phân từ// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
92 và các chữ cái từ// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
93 [hoặc// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
94]. Các chữ cái// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
93 được liên kết với các giá trị// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
96// integer let num = 47; // float let num = 47.32; // exponential - to represent large numbers let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700 // exponential - to represent small numbers let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77 // binary let num = 0b1111; // stands for 15 // octal let num = 023; // stands for 19 // hexadecimal let num = 0xFF; // stands for 255
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Một điều cần lưu ý là các số không được bao quanh bởi dấu ngoặc kép - điều đó sẽ tự động biến chúng thành một chuỗi
// this is a string not a number!
let num = '7';
Làm cách nào để kiểm tra loại dữ liệu của một giá trị trong JavaScript?
Để tránh mắc lỗi và kiểm tra kỹ loại dữ liệu của một giá trị trong JavaScript, hãy sử dụng toán tử
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
97Trước đó tôi đã đề cập rằng các số được đặt trong dấu ngoặc kép là các chuỗi
Bạn có thể tự kiểm tra điều đó bằng cách làm như sau
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
9Cách chuyển đổi một chuỗi thành một số bằng hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Cú pháp chung cho hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 như sau// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
2Hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 nhận hai đối số. một chuỗi làm đối số đầu tiên và một cơ số làm đối số tùy chọn thứ haiChuỗi là giá trị cần chuyển thành số
Cơ số chỉ định hệ thống số toán học mà bạn muốn sử dụng và cơ số của số sẽ được trả về – cho dù số đó sẽ là cơ số 2 [hoặc nhị phân], cơ số 8 [hoặc bát phân], cơ số 10 [thập phân] hay cơ số
Nếu cơ số không được bao gồm, thì nó là
// this is a string not a number!
let num = '7';
2 [giá trị thập phân] theo mặc định// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5Nếu chuỗi chứa chữ cái và số thì sao?
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6Khi
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 gặp một ký tự không phải là số, nó sẽ bỏ qua ký tự đó và tất cả các ký tự theo sau đó, ngay cả khi có nhiều số hơn ở cuối dòngMột điều cần lưu ý là nếu chuỗi không bắt đầu bằng một số, thì thay vào đó, _________ 323 [viết tắt của ________ 324] sẽ được trả về
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
0Hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 sẽ bắt đầu tại vị trí // this is a string not a number!
let num = '7';
3 của chuỗi và xác định xem ký tự tại vị trí đó có thể chuyển đổi thành số hay không. Nếu không thể, thay vào đó, hàm sẽ trả về // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
23, ngay cả khi chuỗi chứa số sau nàyNếu bạn có một chuỗi chứa dấu phẩy thì sao?
// string created using double quotes [""]
let favePhrase = "Hello World!";
0Nếu rơi vào trường hợp này và bạn muốn thực hiện chuyển đổi theo nghĩa đen, tốt nhất bạn nên sử dụng hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 để thay thếCách chuyển đổi một chuỗi thành một số bằng hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Cú pháp chung cho hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 như sau// string created using double quotes [""]
let favePhrase = "Hello World!";
1Cú pháp và hành vi của hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 tương tự như của hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4. Sự khác biệt chính là // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 chỉ nhận một đối số và không chấp nhận cơ số làm đối sốHàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 chấp nhận một chuỗi làm đối số duy nhất của nó và trả về một số float – một số có dấu thập phânSử dụng hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 khi bạn muốn giữ lại phần thập phân chứ không chỉ phần nguyên của một sốLấy ví dụ tương tự từ phần trước, đây là cách bạn sẽ viết lại nó bằng cách sử dụng
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5// string created using double quotes [""]
let favePhrase = "Hello World!";
2Cũng giống như
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4, hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 sẽ chỉ trả về số đầu tiên và bỏ qua mọi ký tự không phải là số// string created using double quotes [""]
let favePhrase = "Hello World!";
3Và cũng giống như
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 một lần nữa, nếu ký tự đầu tiên không phải là số hợp lệ, hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 sẽ trả về // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
23 thay vì một số vì nó không thể chuyển đổi nó thành số// string created using double quotes [""]
let favePhrase = "Hello World!";
4Cách chuyển đổi một chuỗi thành một số bằng hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Cú pháp chung cho hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 như sau// string created using double quotes [""]
let favePhrase = "Hello World!";
5Sự khác biệt giữa hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 và các hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 và // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
5 là hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 cố gắng chuyển đổi toàn bộ chuỗi thành một số cùng một lúc. Các phương thức phân tích cú pháp chuyển đổi một chuỗi thành một số theo từng phần và chúng di chuyển qua các ký tự tạo nên chuỗi riêng lẻ và từng ký tự mộtHãy lấy ví dụ sau mà bạn đã thấy trước đó sử dụng
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4// string created using double quotes [""]
let favePhrase = "Hello World!";
6Thời điểm mà
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 gặp một ký tự không phải là số, nó sẽ kết thúc quá trình chuyển đổiĐây là cách ví dụ tương tự hoạt động với hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6// string created using double quotes [""]
let favePhrase = "Hello World!";
7Vì
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 cố gắng chuyển đổi và đánh máy toàn bộ chuỗi thành một số cùng một lúc, nên nó trả về // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
23 do gặp các ký tự không phải là số và do đó, không thể chuyển đổi thành sốHàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 là một lựa chọn tuyệt vời khi bạn muốn chuyển đổi không thành công nếu chuỗi chứa các ký tự không phải là sốMột điều khác cần lưu ý là hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
6 không trả về một số nguyên khi nó gặp một số thập phân, trái ngược với hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
4 mà bạn đã thấy trước đó// string created using double quotes [""]
let favePhrase = "Hello World!";
8Cách chuyển đổi một chuỗi thành một số bằng hàm // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
7
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Đối tượng
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
7 là một đối tượng JavaScript tích hợp. Và bạn có thể sử dụng một số phương thức của nó, chẳng hạn như // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
09,// string created using double quotes [""]
let favePhrase = "Hello World!";
00 và // string created using double quotes [""]
let favePhrase = "Hello World!";
01, để chuyển đổi chuỗi thành sốMột điều cần lưu ý và ghi nhớ khi sử dụng các phương thức Toán học để chuyển đổi kiểu là khi bạn đang làm việc với số float, chúng sẽ biến chúng thành một số nguyên và số float sẽ mất phần thập phân của nó
Các phương thức sẽ chuyển đổi chuỗi thành số nguyên gần nhất tương đương
Hàm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
09 chuyển đổi một chuỗi thành một số và làm tròn nó thành số nguyên gần nhất// string created using double quotes [""]
let favePhrase = "Hello World!";
9Nếu giá trị của
// string created using double quotes [""]
let favePhrase = "Hello World!";
03 bằng // string created using double quotes [""]
let favePhrase = "Hello World!";
04 thì ta được kết quả như sau// string created using backticks [``]
let favePhrase = `Hello World!`;
0Nếu chuỗi chứa các ký tự không phải là số, ________ 409 trả về ________ 323
// string created using backticks [``]
let favePhrase = `Hello World!`;
1Hàm
// string created using double quotes [""]
let favePhrase = "Hello World!";
00 chuyển đổi một chuỗi thành một số và làm tròn nó xuống số nguyên gần nhất// string created using backticks [``]
let favePhrase = `Hello World!`;
2Nếu chuỗi chứa các ký tự không phải là số, ________ 700 trả về ________ 323. Cách thức hoạt động của hàm này là nó cố gắng chuyển đổi toàn bộ chuỗi thành một số và sau đó đánh giá kết quả, nghĩa là chuỗi phải là một chuỗi hợp lệ để nó hoạt động
// string created using backticks [``]
let favePhrase = `Hello World!`;
3Hàm
// string created using double quotes [""]
let favePhrase = "Hello World!";
01 ngược lại với hàm // string created using double quotes [""]
let favePhrase = "Hello World!";
00 vì nó chuyển đổi một chuỗi thành một số và làm tròn nó lên đến số nguyên gần nhất// string created using backticks [``]
let favePhrase = `Hello World!`;
4Tương tự như các ví dụ trước, hàm
// string created using double quotes [""]
let favePhrase = "Hello World!";
01 sẽ trả về // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
23 khi nó gặp một giá trị không phải là số trong chuỗi// string created using backticks [``]
let favePhrase = `Hello World!`;
5Cách chuyển đổi một chuỗi thành một số bằng cách nhân và chia cho // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Nhân với
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8 là một trong những cách nhanh nhất để chuyển đổi một chuỗi thành một số// string created using backticks [``]
let favePhrase = `Hello World!`;
6Và nếu bạn muốn thực hiện chuyển đổi kiểu trên số float, nhân với
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8 sẽ đặt vị trí thập phân// string created using backticks [``]
let favePhrase = `Hello World!`;
7Nếu chuỗi chứa các ký tự không phải là số, nó sẽ trả về ________ 323
// string created using backticks [``]
let favePhrase = `Hello World!`;
8Cách chuyển đổi chuỗi thành số nguyên này cũng hoạt động với việc chia chuỗi cho
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8// string created using backticks [``]
let favePhrase = `Hello World!`;
9Tại thời điểm này, cũng cần đề cập đến điều gì sẽ xảy ra khi bạn cố gắng thêm
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8 vào một chuỗi để chuyển đổi nó thành một số nguyên. Nếu bạn cố gắng làm điều đó, đây là kết quả bạn sẽ nhận được// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
0Trong ví dụ trên,
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
8 được nối với chuỗi // string created using double quotes [""]
let favePhrase = "Hello World!";
21, nghĩa là nó được đặt cạnh chuỗiCách chuyển đổi một chuỗi thành một số bằng cách sử dụng toán tử // integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
9 đơn nguyên
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
Sử dụng toán tử
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
9 đơn nguyên cũng là một trong những cách nhanh nhất để chuyển đổi một chuỗi thành một sốBạn đặt toán tử cộng,
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
9, trước chuỗi và nó sẽ chuyển đổi chuỗi thành một số nguyên// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
1hoặc một cái phao
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
2Tương tự như những cách khác mà bạn đã thấy để chuyển đổi một chuỗi thành một số, toàn bộ chuỗi chỉ được chứa các ký tự số để toán tử một ngôi ________ 09 hoạt động. Nếu chuỗi không đại diện cho một số, thì nó sẽ trả về
// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
23// integer
let num = 47;
// float
let num = 47.32;
// exponential - to represent large numbers
let num = 477e2; // equal to multiplying 477 to 10 to the power of 2 [or 100] which results in 47700
// exponential - to represent small numbers
let num = 477e-2; // equal to dividing 477 to 10 to the power of 2 [or 100] which results in 4.77
// binary
let num = 0b1111; // stands for 15
// octal
let num = 023; // stands for 19
// hexadecimal
let num = 0xFF; // stands for 255
3Sự kết luận
Và bạn có nó rồi đấy. Bây giờ bạn đã biết một số cách để chuyển đổi chuỗi thành số trong JavaScript
Để tìm hiểu thêm về JavaScript, hãy đến với Chứng nhận cấu trúc dữ liệu và thuật toán JavaScript của freeCodeCamp
Đó là một chương trình giảng dạy miễn phí, được cân nhắc kỹ lưỡng và có cấu trúc, nơi bạn sẽ học một cách tương tác. Cuối cùng, bạn cũng sẽ xây dựng 5 dự án để nhận chứng chỉ và củng cố kiến thức của mình
Cảm ơn vì đã đọc
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Học một cái gì đó mới mỗi ngày và viết về nó
Nếu bài viết này hữu ích, hãy tweet nó
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu