Để biết liệu một Chuỗi cụ thể có chứa một từ đã cho hay một chuỗi con hay không, bạn có thể sử dụng phương thức indexOf[] trong JavaScript
Chuỗi. nguyên mẫu. Chỉ số[]. The indexOf method will return the index of the calling String object, it will return the first occurrence of the provided substring value. It will return -1 if the provided substring is not found
Example.JavaScript Substring example:
This is some text and i want to find if the work
apple is in this string or not!
This string does not contains that word!
var val1 = document.getElementById["myString1"].innerHTML.indexOf["apple"];
var val2 = document.getElementById["myString2"].innerHTML.indexOf["apple"];
if[val1 > -1] {
console.log["myString1 contains the word apple!"];
} else {
console.log["myString1 does not contain the word apple!"]
}
if[val2 > -1] {
console.log["myString2 contains the word apple!"];
} else {
console.log["myString2 does not contain the word apple!"]
}
Output
"myString1 contains the word apple. "
"myString2 does not contain the word apple. "
There are four parts in a for-loop. Three of them, initialization, test and post-processing, are enclosed in brackets
My First JavaScript says73 and separated by 2 semicolons. The body contains the repetitive task to be performed. The initialization statement is first executed. The test is then evaluated. If the test returns true, the body is executed; followed by the post-processing statement. The test is evaluated again and the process repeats until the test returns false. When the test is false, the for-loop completes and program execution continues to the next statement after the for-loop. The following flow chart illustrates the for-loop processHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
In this example, the variable
My First JavaScript says74 is initialized to 1. IfHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says74 is less than or equal to 100, the body of the loop executes, followed by the post-processing statement, which increment the value ofHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says74 by 1. The loop repeats until the value ofHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says74 is NOT less than or equal to 100 [i. e. , more than 100]TRYHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Modify the above script to prompt the user for the multiplier as well as the number of multiples to be printed [in two
My First JavaScript says
15 statements]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Modify the above script to print only multiples that are odd number. [Hint. The modulo operator "
My First JavaScript says
79" can be used to compute the remainder, e. g. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
80 computes the remainder ofHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
81 divides by 2, which results in either 0 or 1. ]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JS phía máy khách EG 5. Hàm do người dùng định nghĩa và Trình xử lý sự kiện My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
82
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Bên cạnh các hàm có sẵn trong JavaScript như
for [ initialization ; test ; post-processing ] { body ; }8,
My First JavaScript says15,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says85 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says57, bạn có thể xác định các hàm của riêng mình. Một hàm có tên và phần thân bao gồm một tập hợp các câu lệnh JavaScript cùng thực hiện một nhiệm vụ nhất định. Nó có thể lấy 0 hoặc nhiều đối số từ người gọi và trả lại 0 hoặc một giá trị cho người gọiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Làm thế nào nó hoạt động?- Các dòng 8-10 định nghĩa một hàm có tên là
My First JavaScript says
87, thông qua từ khóaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
88. The function invokes the built-in functionHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
89, which opens a new browser window [or tab] and loads the page "Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
90"Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 15-16 tạo nút HTML. Clicking the button triggers the
My First JavaScript says
82 event handler, i. e. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
87 được xác định trước đóHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
CỐ GẮNG. Include another button, which opens "
My First JavaScript says93"Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Client-Side JS EG 6. Thêm Trình xử lý sự kiện. My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
94, My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 and My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
96
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript can be used to handle many types of events, in response to a user's action or browser's action. For example,
My First JavaScript says
94. fires after browser loaded the pageHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
95 andHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
96. fires when the user points the mouse pointer at/away from the HTML elementHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
Dissecting the Program- Line 8 defines a variable
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
00, which holds the strings to be displayed in theMy First JavaScript says
94 event handlersHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- In the opening tag [Line 12], we define the
My First JavaScript says
94 event handler for theHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
03 event. It invokesfor [ initialization ; test ; post-processing ] { body ; }
8 with the message defined earlier - Line 13 and 14 defines the event handlers
My First JavaScript says
95 andHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
96 for the HTML elementHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The text's color will be changed to red when the user points the mouse pointer at the element [by setting the CSS style property
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
07 to red], and revert back to its original color when the mouse pointer is moved away [by resetting the CSS style propertyalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
07 to an empty string]. The special keywordalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
09 refer to this object
Client-Side JS EG 7. Separating HTML, CSS and JavaScript
The previous example works fine. You will find many such example in textbooks, especially the older textbooks. However, it has a big problem. All the HTML contents, CSS presentation styles and JavaScript programming codes are placed in a single file. For a small toy program, the problem is not serious. But when your program grows and if the HTML, CSS and JavaScript are written by different people, you will have a real challenge in maintaining the program. Remember that HTML is for the contents, CSS for presentation and JavaScript for the behavior
Let's rewrite the example to place the HTML, CSS and JavaScript in three different files
My First JavaScript says0Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says1Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says2Làm thế nào nó hoạt động?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Placing the scripting codes inside the HTML page is not a good software engineering practice. Instead, the now-preferred approach is to place the scripts, as well as CSS styles, in external files, which can then uniformly applied to all pages in your website
- Let's begin with the HTML file. Now, the HTML file keeps only the contents, no presentation style and nor programming logic
The CSS style sheet is kept in an external file, referenced via the above
6 tagJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Note. In HTML4/XHTML1. 0 you need to include attributealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
11 in the
6 opening tagJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Also, the JavaScript programming code is kept in an external file, referenced via the above
6 is needed, although there is no contentJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Note. In HTML4/XHTML1. 0, you need to include attribute
7 in theJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
6JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
My First JavaScript says
3How it works?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- This HTML document contains a element with an unique
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
16, twoelements, three
elements with
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
17, and three
6 with uniqueJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
19 - In the user-defined function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
20, we usealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
21 to select the element, and then modify itsalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
22 property - In
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
23 function, we usealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
24 to select all theelements in an array
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
25. We then use a for-loop to iterate through all the elements in the array. Thealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
26 property keeps the length of the array - In
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
27 function, we usealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
28 to select all theyếu tố. We use JavaScript's
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
29 loop to iterate through all the elements. We use the safestif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8-variable, asalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
29 loop create a new block-scope variable for each iteration; but the traditionalalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
32 does not - The page contains three buttons to trigger the functions defined
- in order to manipulate elements in the DOM using JavaScript, the JavaScript code must be run after the relevant element has been created in the document. This can be done by putting the JavaScript just before the ending tag
- The script contains a function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33, which is assigned as theMy First JavaScript says
94 handler viaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
35. That is,alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 will be triggered after the page is loaded. Thealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 function assignsMy First JavaScript says
82 event handlers to the buttons, selected viaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
39 using the unique buttons'alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
19 - Instead of
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
22 property, older versions of JavaScript usealert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
42 [which includes both texts and markups] to modify the content of the selected element. According to MDN. "alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
22 should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids an XSS [Cross-Site Scripting] attack vector. "
Client-Side JS EG 9. Chặn một siêu liên kết
This example uses a script to intercept a hyperlink to put up a warning message, then proceed to the link
My First JavaScript says
4Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
5How it works?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- The HTML page has an hyperlink
6with an uniqueJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
19 - The
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 function assigns anMy First JavaScript says
82 handler to the hyperlink, selected viaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
39 - When the hyperlink is clicked, the
My First JavaScript says
82 handlerHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
50 triggered. IfMy First JavaScript says
16 returns true, the new page will be loaded; otherwise, the current page remainsHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
More Advanced JavaScript Examples
More "Advanced JavaScript Examples"
Debugging JavaScripts
A graphic debugger is a MUST in programming. Programming in JavaScript, in particular, requires a good debugger. It is because JavaScripts are interpreted and does not need to be compiled. Hence, there is no compiler to show you the syntax errors. A simple and trivial syntax error [e. g. , missing bracket, mis-spelling] will render the entire script not workable. Worst still, you would not see any error message when the script did not work. Can you debug without any error message or clue?
Without a graphic debugging, the only mean to debug JavaScript is to insert
for [ initialization ; test ; post-processing ] { body ; }
8 at strategic locations to print out selected dataAfter modifying a JavaScript, I recommend that you use Ctrl-F5 to refresh the page, which shall load a fresh copy instead of loading from the cache. Bạn có thể cần xóa bộ nhớ cache của trình duyệt hoặc khởi động lại trình duyệt nếu sửa đổi của bạn không có hiệu lực
F12 Web Developer ToolsIn Chrome and Firefox, you can press F12 to activate the developer Tools [hence called F12 Developer Tools]
It provides these features
- Console. View the JavaScript error messages. Start the firebug and switch to the "Console" panel
- Script. View and debug JavaScript. Bắt đầu con bọ lửa. Chuyển sang bảng "Script". "Bật" hoặc "Tải lại" nếu cần. Bạn có thể đặt điểm dừng bằng cách nhấp vào số câu lệnh, thực hiện một bước qua các câu lệnh JavaScript, xem biểu thức, v.v.
- DOM. Xem DOM HTML của tài liệu hiện tại
- HTML và CSS
Để gỡ lỗi JavaScript
- Khởi chạy Firebug ⇒ Chọn bảng "Tập lệnh" để xem JavaScript của bạn, được hiển thị bằng số dòng in đậm màu xanh lá cây
- Làm mới [Ctrl-F5] trang và kiểm tra bảng điều khiển lỗi [Trong "Bảng điều khiển" ⇒ "Lỗi"] để biết các lỗi cú pháp. Sửa tất cả các lỗi cú pháp
- Để theo dõi chương trình, hãy đặt các điểm dừng tại các câu lệnh JavaScript đã chọn, bằng cách nhấp vào lề phải [ở bên trái của số dòng]. A red circle shows up denoting a breakpoint. Take note that you can only set breakpoint on statements with a green-bold line number. [Nếu các câu lệnh JavaScript của bạn không có số dòng in đậm màu xanh lá cây, thì có lỗi cú pháp trên các câu lệnh này. Bạn cần sửa lỗi cú pháp và tải lại trang. ]
- Kích hoạt tập lệnh [thông qua nhấp vào nút/liên kết hoặc tải lại trang]. Việc thực hiện dừng tại điểm dừng đầu tiên. Sau đó, bạn có thể chuyển qua câu lệnh [hoặc bước vào chức năng] và kiểm tra các biến bằng cách định vị con trỏ chuột trên biến;
- Bạn có thể tiếp tục thực hiện [thông qua nút tiếp tục]
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
53Thay vì sử dụng
for [ initialization ; test ; post-processing ] { body ; }
8 vàfor [ initialization ; test ; post-processing ] { body ; }
9 để hiển thị kết quả trung gian, bạn cũng có thể sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
56, viếtalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
57 và giá trị củaalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
58 vào bảng điều khiển lỗi. Nó không can thiệp vào sự xuất hiện của trang web cũng như hoạt động bình thường của bạnVí dụ: sửa đổi JavaScript trong Ví dụ 8 để in các giá trị của InternalHTML trước khi sửa đổi. Bạn cần bật bàn điều khiển [nhấn F12] để xem đầu ra
My First JavaScript says
6Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript và HTML
Như đã thấy từ các ví dụ trước, JavaScript được nhúng bên trong tài liệu HTML và được trình duyệt thực thi. Có hai trường hợp mà trình duyệt thực thi các lệnh JavaScript
- JavaScript đính kèm trong
6 chạy trong khi tải trangJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Một số JavaScript, được gọi là trình xử lý sự kiện, chạy do hành động [hoặc sự kiện] của người dùng hoặc trình duyệt. Ví dụ: nhấp vào nút [
My First JavaScript says
82] hoặc tải trang [Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
94]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Do đó, có hai nơi để đặt JavaScript của bạn
- giữa các thẻ chứa
6;JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- bên trong các thẻ HTML dưới dạng trình xử lý sự kiện [chẳng hạn như
My First JavaScript says
82,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
95,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
94], e. g. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
66
6 chứa các câu lệnh lập trình JavaScript. Ví dụ,JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Trong HTML4/XHTML1. 0, bạn cần bao gồm thuộc tính
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
68 trong
6 là cần thiết mặc dù không có nội dung. ?JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Trong HTML4/XHTML1. 0, bạn cần bao gồm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
68 trong các thẻ
6 và chạy nó dưới trình duyệt web, bạn sẽ cần kích hoạt trình gỡ lỗi [Công cụ dành cho nhà phát triển web] để xem kết quả. Xem phần trên về các ví dụ phía máy kháchJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
JS độc lập Ví dụ 2. [LÀM]- This HTML document contains a element with an unique
- Nhận xét HTML được đính kèm bên trong
- CSS hỗ trợ chú thích nhiều dòng
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
75, nhưng KHÔNG hỗ trợ chú thích cuối dòngMy First JavaScript says
49Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- JavaScript hỗ trợ cả
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
75 vàMy First JavaScript says
49, như Java/C/C++/C#Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Mã định danh có thể chứa các chữ cái [_______59_______92,
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
93], chữ số [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
94], dấu gạch dưới [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
95] và ký hiệu đô la [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
96]. Nhưng nó không thể bắt đầu bằng một chữ số [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
94] - Mã định danh phân biệt chữ hoa chữ thường. Một
0 KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
2, và KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
1JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Mã định danh không thể là từ khóa
- Hãy lưu ý rằng KHÔNG được phép có dấu gạch ngang [
01] và khoảng trắng. Do đó,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
02,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
03, KHÔNG phải là định danh hợp lệ. Bạn nên sử dụng dấu gạch dưới [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
95] thay vì dấu gạch nối [
01]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- chuỗi. một dãy ký tự. Các chuỗi ký tự được đặt trong một cặp dấu nháy đơn hoặc nháy kép [e. g. ,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- số. lấy cả hai số nguyên [e. g. ,
06] hoặc dấu phẩy động [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
15]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean. lấy nghĩa đen boolean của một trong hai
My First JavaScript says
26 hoặcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27 [viết thường]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- chưa xác định. nhận một giá trị theo nghĩa đen đặc biệt được gọi là
18. Lưu ý rằngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 vừa là loại vừa là giá trị theo nghĩa đenJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- symbol [ES6]. Một kiểu dữ liệu có thể hiện là duy nhất và không thay đổi
- bigint [ES2020/ES11]. số nguyên với độ chính xác tùy ý
- mục tiêu. cho các đối tượng chung
- hàm số. cho các đối tượng chức năng. Không giống như Java, hàm là một đối tượng hạng nhất trong JavaScript, e. g. , bạn có thể gán một hàm cho một biến
- vô giá trị. Một giá trị theo nghĩa đen đặc biệt cho chưa phân bổ [chưa xây dựng]
20. Hãy lưu ý rằngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
21 KHÔNG phải làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
23 làJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
20.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
21 có nghĩa là đại diện cho sự vắng mặt của một đối tượng được xây dựng.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21 thực sự được coi là nguyên thủy vì nó không có thuộc tính và phương thứcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Số thập phân. bắt đầu bằng một chữ số
47 đếnJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
48 [không phảiJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
49], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
50 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
51,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- bát phân. bắt đầu bằng một chữ số
49, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
53 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
54,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- thập lục phân. bắt đầu bằng
55 [hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
56], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
57 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
58JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- nhị phân. bắt đầu bằng
59 [hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
60], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
61 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
62. [Nhị phân có thể không được hỗ trợ trong một số trình duyệt. ]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- vô cực. e. g. ,
67JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- -vô cực. e. g. ,
68JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- NaN [Không phải là số]. e. g. ,
69 hoặc chuyển đổi chuỗiJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
70 thành một sốJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- parseInt[str], parseFloat[str]. Phân tích cú pháp
3 cho đến số không phải chữ số đầu tiên và trả về số;JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Toán học. vòng[số], Toán. sàn[số], Toán. trần nhà [số]
- Toán học. ngẫu nhiên[]. Tạo một số ngẫu nhiên giữa 0 [bao gồm] và 1 [độc quyền]
- isNaN[str]. trả về true nếu
3 không phải là số. Ví dụ: Thật thú vị khi lưu ý rằng JavaScript không có các chức năng đối ứng nhưJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
93,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
94JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Số[str]. Trả về số được đại diện bởi
My First JavaScript says
25 hoặcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91. Cho rằng tên hàm này bắt đầu bằng chữ hoa, bởi vì đây là thao tác truyền kiểuJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- toFixed[decimalPlaces]. Trả lại số/chuỗi này về số vị trí thập phân đã cho. Ví dụ,
- chiều dài. e. g. ,
48 trả về độ dài của chuỗiJavaScript Example: The Date object
- Đến trường hợp trên[]. trả về chuỗi chữ hoa
- toLowerCase[]. trả về chuỗi chữ thường
- charAt[idx]. trả về ký tự ở vị trí
49. Index begins from 0. Negative index can be used, which counts from the end of the stringJavaScript Example: The Date object
- substring[beginIdx, endIdx]. returns the substring from
50 [inclusive] toJavaScript Example: The Date object
51 [exclusive]JavaScript Example: The Date object
- substr[beginIdx, length]. returns the substring from
50 ofJavaScript Example: The Date object
53JavaScript Example: The Date object
- indexOf[searchStr, fromIdx?]. Return the beginning index of the first occurrence of
54, starting from an optionalJavaScript Example: The Date object
55 [default of 0]; orJavaScript Example: The Date object
56 if not foundJavaScript Example: The Date object
- lastIndexOf[searchStr, fromIdx?]. Return the beginning index of the last occurrence of
54JavaScript Example: The Date object
6, starting from an optionalJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
55 [default ofJavaScript Example: The Date object
60]; orJavaScript Example: The Date object
56 if not foundJavaScript Example: The Date object
- slice[beginIdx, endIdx]. Return the substring from
62 [inclusive] toJavaScript Example: The Date object
63 [exclusive]JavaScript Example: The Date object
- repeat[count] [ES6]. repeat the string
- split[delimiter], array. join[delimiter]. returns an array by splitting the string using
64, e. g. ,JavaScript Example: The Date object
- Searching/Modifying Strings using Regular Expression. See ""
- charAt[idx]. For example,
- Array Bracket Operator []. You can treat a string as a character array, and access individual character via array bracket operator
65, e. g. ,JavaScript Example: The Date object
- number
49JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- number
91 [Not-a-Number],JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- empty string [
13,JavaScript Example: The Date object
88],JavaScript Example: The Date object
18 value,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean
My First JavaScript says
27,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21 [unallocated] objectHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
[TODO] Kiểm tra các Hạn chế như Tệp IO, Mạng, v.v. So sánh với Shell Script và Python script để tự động hóa
Ví dụ JavaScript phía máy chủ
Bạn cũng có thể sử dụng JavaScript để lập trình ứng dụng web phía máy chủ, giống như Java Servlet/JSP, Microsoft ASP, PHP, Python, v.v. Điểm hấp dẫn là bạn có thể sử dụng một ngôn ngữ duy nhất để lập trình cả phía máy khách và phía máy chủ trong ứng dụng web
Xem nút. bài viết js
Cú pháp cơ bản JavaScript
Bình luận
Các nhận xét bị thời gian chạy JavaScript bỏ qua nhưng rất hữu ích trong việc giải thích mã của bạn cho người khác [và cả cho chính bạn ba ngày sau]. Bạn nên sử dụng nhận xét một cách tự do để giải thích hoặc ghi lại mã của mình
Nhận xét cuối dòng bắt đầu bằng
My First JavaScript says
49 và kéo dài đến cuối dòng hiện tại. Nhận xét nhiều dòng bắt đầu bằngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
73 và kéo dài đếnalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
74lưu ý rằng
Khoảng trắng [trống, tab, dòng mới]
Giống như C/C++/Java, JavaScript bỏ qua các khoảng trắng bổ sung [khoảng trống, tab và dòng mới]. Tôi thực sự khuyên bạn nên sử dụng các khoảng trắng bổ sung để định dạng chương trình của mình nhằm làm cho mã của bạn dễ đọc và dễ hiểu hơn
Biểu thức
Một biểu thức là sự kết hợp của các biến, ký tự, toán tử và biểu thức con có thể được đánh giá để tạo ra một giá trị duy nhất
Câu lệnh, Dấu chấm phẩy và Khối
Một tuyên bố là một hướng dẫn lập trình duy nhất. Không giống như C/C++/Java, nơi bạn cần kết thúc câu lệnh bằng dấu chấm phẩy [
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
79], trong JavaScript, dấu chấm phẩy là tùy chọn. Tuy nhiên, nếu thiếu dấu chấm phẩy, bạn cần kết thúc câu lệnh bằng một dòng mới [và công cụ JavaScript sẽ chèn dấu chấm phẩy cho bạn]Dấu chấm phẩy trong JavaScript chia rẽ cộng đồng. Một số thích sử dụng chúng luôn luôn, không có vấn đề gì. Những người khác muốn tránh chúng cho ngắn gọn. tôi sẽ để nó cho bạn
Một khối bao gồm 0 hoặc nhiều câu lệnh được đặt trong một cặp dấu ngoặc nhọn
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
80. Không cần dấu chấm phẩy sau dấu ngoặc nhọnBiến, chữ và kiểu
Khai báo biến [My First JavaScript says
29,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8] và Toán tử gán [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
84]Một biến là một vị trí lưu trữ được đặt tên chứa một giá trị. Trước ES6, bạn chỉ có thể sử dụng
My First JavaScript says
29 để khai báo biến toàn cục. ES6 giới thiệu hai từ khóa mới.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 để khai báo một biến phạm vi khối cục bộ vàif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 để khai báo một hằng số phạm vi khối cục bộBạn nên thử
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [an toàn nhất], tiếp theo làif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 và tránhMy First JavaScript says
29Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Bạn có thể gán [và gán lại] một giá trị cho một biến bằng toán tử gán [
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
84]. Ví dụ,định danhMã định danh là tên được đặt để xác định các thực thể [chẳng hạn như biến và hàm]. Các quy tắc cho số nhận dạng hợp lệ là
Một chữ là một giá trị cố định, e. g. ,
06,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
07,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
09,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
26,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21, có thể được gán cho một biến hoặc tạo thành một phần của biểu thứccác loạiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript là hướng đối tượng. Nhưng, Nó hỗ trợ cả các kiểu và đối tượng nguyên thủy
Nguyên thủy không phải là đối tượng và không sở hữu các thuộc tính và phương thức. JavaScript hỗ trợ các kiểu nguyên thủy này
JavaScript cũng hỗ trợ các loại đối tượng và giá trị này [chúng ta sẽ thảo luận về đối tượng sau]
Không giống như hầu hết các ngôn ngữ lập trình chung [chẳng hạn như Java/C/C++/C#] là loại mạnh, JavaScript là loại lỏng lẻo [tương tự như hầu hết các ngôn ngữ kịch bản như UNIX Shell Script, Perl, Python]. Bạn không cần phải khai báo rõ ràng loại biến [chẳng hạn như
27 vàJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
28] trong khi khai báo. Loại được quyết định khi một giá trị được gán cho biến đó. Nếu một số được gán, thì biến đó có kiểuJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74 và có thể thực hiện các phép toán số như cộng và trừ. Nếu một chuỗi được gán, biến đó sẽ có kiểuHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30 và có thể thực hiện các thao tác chuỗi như nối chuỗi. Nói cách khác, loại được liên kết với giá trị, thay vì biếnNgười điều hànhJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Bạn có thể sử dụng toán tử
31 để kiểm tra loại giá trị hiện tại được gán cho một biến.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31 trả về một chuỗiJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Ví dụ,
Loại
18 và giá trị theo nghĩa đen củaJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Một biến không được khai báo [thông qua từ khóa
My First JavaScript says
29] có một loại đặc biệt gọi làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18. Bạn không thể tham khảo giá trị của nóJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Khi một biến được khai báo [thông qua từ khóa
My First JavaScript says
29] mà không gán giá trị ban đầu, nó sẽ có kiểuHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18 và giữ một giá trị đặc biệt có tên làJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
41 có lẽ chính xác hơn?. ], ngay sau khi một giá trị được gán, biến sẽ nhận kiểu của giá trị đó. Hành động đặt một giá trị vào một biến đặt kiểu của nó. Bạn có thể thay đổi loại biến bằng cách gán lại giá trị của loại khác. Nói cách khác, loại được liên kết với giá trị, không phải biến. Các loại được chuyển đổi tự động khi cần trong quá trình thực thi [được gọi là kiểu động]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Ví dụ như,
[ES6] Hằng sốif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8-khai báoBạn có thể tạo một hằng số chỉ đọc, được đặt tên với từ khóa
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [thay choMy First JavaScript says
29]. Ví dụ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
7Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Loại
My First JavaScript says
74, Chữ & Hoạt độngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Một biến kiểu
My First JavaScript says
74 chứa một số, có thể là số nguyên hoặc số dấu phẩy độngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Chữ số nguyên có thể được biểu thị bằng
Các chữ số dấu phẩy động có thể được biểu diễn ở dạng thông thường [e. g. ,
63] hoặc ký hiệu khoa học, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
64,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
65JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
JavaScript cũng cung cấp một số
My First JavaScript says
74 chữ đặc biệtHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Các phép toán số học, như được lập bảng dưới đây, có thể được áp dụng cho các số. Các kết quả sau đây thu được với giả định rằng
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72 trước khi phẫu thuậtOperatorDescriptionExampleResult [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72]+Additionz = x + y;z is 7-Subtraction [or Unary Negation]z = x - y;z is 3*Multiplicationz = x * y;z is 10/Divisionz = x / y;z is 2. 5%Modulus [Phần còn lại của phép chia]z = x % y;z is 1++Unary Pre- or Post-Incrementy = x++;JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Giống như. y = x;
Giống như. x=x-1;
[ES7]y ** xTrong JavaScript, các phép toán số học luôn được thực hiện ở dạng dấu phẩy động có độ chính xác kép [KHÔNG phải là số nguyên]. Tức là,
75 cho ____76_______76 [thay vì 0 trong Java/C/C++]. Bạn có thể sử dụng hàm có sẵnJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
77 để cắt một giá trị dấu phẩy động thành một số nguyên, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
78 vàJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
79 mang lại choJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
80. Bạn cũng có thể sử dụng các hàm toán học có sẵn nhưJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
81,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
82,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
83 để chuyển đổi một số dấu phẩy động thành một số nguyênToán Tử Số Mũ [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
0]JavaScript Example: Loop
Testing Loop
ECMAScript 2016 [ES7] giới thiệu toán tử số mũ [
0], bạn có thể viếtJavaScript Example: Loop
Testing Loop
86, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
87JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Trước ES7, bạn phải sử dụng chức năng tích hợp sẵn
88Toán tử số học kiêm toán tử gánJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Đây là những toán tử tay ngắn để kết hợp hai phép toán
Toán tử Mô tảVí dụResult+=Bổ sung kiêm Phép gánx += y;Giống như. x = x + y;-=Phép trừ kiêm phép tínhx -= y;Giống như. x = x - y;*=Phép nhân kiêm phép tínhx *= y;Giống như. x = x * y;/=Phép chia kiêm phép tínhx /= y;Giống như. x = x / y;%=Mô-đun kiêm Bài tậpx %= y;Giống như. x = x % y;Một số Hàm tích hợp
89 thường được sử dụngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 Loại, Chữ & Hoạt độngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Một
30 là một dãy các ký tự được đặt trong một cặp dấu nháy đơn hoặc nháy kép [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. ES6 giới thiệu các chuỗi mẫu nhiều dòng được phân tách bằng dấu ngoặc kép [trong phần tiếp theo]. Bạn có thể sử dụng một chuỗi thoát để thể hiện các ký tự đặc biệt không in được [chẳng hạn nhưJavaScript Example: The Date object
My First JavaScript says
58 cho dòng mới,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 cho tab vàJavaScript Example: The Date object
05 cho ký tự Unicode]; . g. ,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08]JavaScript Example: The Date object
Không giống như Java/C/C++, nhưng giống như các thuộc tính của HTML/CSS, bạn có thể sử dụng dấu ngoặc đơn hoặc dấu ngoặc kép cho
30. Điều này rất tiện lợi vì bạn có thể sử dụng dấu nháy đơn nếuJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 chứa dấu nháy kép [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
11JavaScript Example: The Date object
11], mà không sử dụng trình tự trốn thoát vụng về [e. g. ,JavaScript Example: The Date object
13]JavaScript Example: The Date object
JavaScript là kiểu động và tự động thực hiện chuyển đổi kiểu. Khi giá trị
30 được sử dụng trong các phép toán số học [chẳng hạn như phép trừ hoặc phép nhân], thời gian chạy JavaScript sẽ tự động chuyển đổi ______76_______30 thành ____46_______74 nếu nó đại diện cho một ____46_______74 hợp lệ; . Ví dụ,Toán tửJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
00. Bổ sung hoặc nối?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Nếu cả hai toán hạng của toán tử
My First JavaScript says
00 làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22, nó sẽ thực hiện phép cộng số thông thường. Tuy nhiên, nếu một [hoặc cả hai] toán hạng làJavaScript Example: The Date object
30, thì toán tửJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
00 bị quá tải để thực hiện nối chuỗi. Toán hạng khác sẽ được chuyển đổi thànhHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30, nếu cần. Ví dụ,Chuyển đổi một sốJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 thànhJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29 vàJavaScript Example: The Date object
30JavaScript Example: The Date object
Để chuyển đổi một số
30 thành một sốJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74, bạn có thể sử dụng các hàm tích hợp sẵnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29, hàm này sẽ trả về một sốJavaScript Example: The Date object
My First JavaScript says
74 nếu chuyển đổi thành công; . Ví dụ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hãy lưu ý rằng
77 hoạt động miễn làJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 bắt đầu bằng các chữ số. Nó sẽ phân tích đến chữ số đầu tiên. Ví dụ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Bạn cũng có thể sử dụng hàm
30 để chuyển đổi đối số đối tượng thành một số đại diện cho giá trị của đối tượng; . Ví dụ,Xây dựng/Chuyển đổi thànhJavaScript Example: The Date object
30.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42 andJavaScript Example: The Date object
43JavaScript Example: The Date object
Bạn có thể xây dựng một
30 từ một loại khác thông qua hàmJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42, e. g. ,JavaScript Example: The Date object
Bạn có thể sử dụng
43 để có được một đại diệnJavaScript Example: The Date object
30 của một đối tượng, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Bạn cũng có thể chuyển đổi một số thành chuỗi bằng cách nối với một chuỗi rỗng, e. g. ,
Thuộc tính của chuỗiJavaScript does not have a dedicated character type, but treats it as a one-character single. To get an individual character from a string, you can use
[ES6] Multi-line Back-Quoted Template String and Substitution
Prior to ES6, You can enclosed a string literal using either single or double quotes [e. g. ,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. Escape sequences are to be used for special characters [e. g. ,JavaScript Example: The Date object
My First JavaScript says
58 for new-line,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 for tab] and resolving conflict [e. g. ,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08]. Multi-line strings are not supportedJavaScript Example: The Date object
ES6 introduces template string, delimited by back-quotes, for example,
There is no need to escape single or double quote inside a template string [because there is no conflicts]. But you need to use escape sequence for back-quote, i. e. ,
75JavaScript Example: The Date object
Template strings support multi-line strings. All whitespaces [blank, tab, newline] within the back-quotes are part of the multi-line string. For example,
Substitution
76JavaScript Example: The Date object
Template strings support substitution, which allow you to embed any valid JavaScript expression as part of the string in the form of
76, for example,JavaScript Example: The Date object
Multi-line template string with substitution provides a more convenient way to write out formatted HTML fragment. For example,
My First JavaScript says
8Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The
78 Type, Literals & OperationsJavaScript Example: The Date object
A variable of the type
78 holds a value of eitherJavaScript Example: The Date object
My First JavaScript says
26 ofHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
26 andHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27 are keywords in JavaScriptHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
As mentioned, JavaScript performs automatic type conversion if necessary. During the type conversion, the following 6 values are converted to
My First JavaScript says
27Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
All the other values are converted to
My First JavaScript says
26Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
You need to memorize this.
93 returnJavaScript Example: The Date object
My First JavaScript says
26 ifHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 is one of these 6 values.JavaScript Example: The Date object
49,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
91,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: The Date object
18,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
27, andHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21Comparison [Relational] OperatorsHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The following relational operators, which produce a
78 result, are defined in JavaScript. The results are obtained assumingJavaScript Example: The Date object
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04OperatorDescriptionExampleResult
[if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04]==Abstract Equal To
[in Value]My First JavaScript says
9Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
0. =Abstract Not Equal To===Strictly Equal To
[in Type and Value]alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
1alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
2. ==Strictly Not Equal To>Greater Than>=Greater Than or Equal To