Đang chuẩn bị liên kết để tải về tài liệu:
Học JavaScript qua ví dụ part 13
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Số lượng, String, hoặc Boolean? Chuyển đổi kiểu dữ liệu Theo định nghĩa trước đó, JavaScript là một ngôn ngữ lỏng lẻo nhập, mà thực sự có nghĩa là bạn không cần phải được quan tâm về những loại dữ liệu được lưu trữ trong một | 112 Chapter 5 Operators EXPLANATION continued 2 When the bitwise OR operator is applied to 1111 1001 the result is binary 1111 or decimal 15. 3 When the bitwise A Exclusive OR is applied to 1111 a 1001 the result is binary 0110 or decimal 6. 4 9 2 yields 36 because 1001 shifted two bits to the left becomes 100100 which is decimal 36. 5 9 2 yields 2 because 1001 shifted two bits to the right becomes 10 which is decimal 2. 6 9 2 yields 3 because the sign is preserved. 7 15 2 yields 3 because 1111 shifted two bits to the right becomes 0011 which is decimal 3. For nonnegative numbers zero-fill right shift and sign-propagating right shift yield the same result. See Figure 5.19. Testing Bitwise Operators 15 9 yields 9 15 9 yields 15 15 A 9 yields 6 9 2 yields 36 9 2 yields 2 -9 2 yields -3 15 2 yields 3 Figure 5.19 Output from Example 5.13. 5.3 Number String or Boolean Data Type Conversion As defined earlier JavaScript is a loosely typed language which really means that you don t have to be concerned about what kind of data is stored in a variable. You can assign a number to x on one line and on the next line assign a string to x you can compare numbers and strings strings and Booleans and so on. JavaScript automatically converts values when it assigns values to a variable or evaluates an expression. If data types are mixed i.e. a number is compared with a string a Boolean is compared with a number a string is compared with a Boolean JavaScript must decide how to handle the expression. Most of the time letting JavaScript handle the data works fine but there are times when you want to force a conversion of one type to another. For example if you prompt a user for input the input is set as a string. But suppose you want to perform calculations on the incoming data making it necessary to convert the strings to numbers. When using the operator you want to add two numbers that have been entered as strings not concatenate them so you will then need to convert the data from .