Javascript kiểm tra xem Internalhtml có chứa chuỗi không

Để 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 says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

73 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 process

In this example, the variable

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is initialized to 1. If
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is less than or equal to 100, the body of the loop executes, followed by the post-processing statement, which increment the value of
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 by 1. The loop repeats until the value of
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is NOT less than or equal to 100 [i. e. , more than 100]

TRY
  1. 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
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    15 statements]
  2. Modify the above script to print only multiples that are odd number. [Hint. The modulo operator "
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    79" can be used to compute the remainder, e. g. ,
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    80 computes the remainder of
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    81 divides by 2, which results in either 0 or 1. ]

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

Bên cạnh các hàm có sẵn trong JavaScript như

for [ initialization ; test ; post-processing ] {
   body ;
}
8,
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

15,
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

85 và
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

57, 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ọi





  
  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
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    87, thông qua từ khóa
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    88. The function invokes the built-in function
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    89, which opens a new browser window [or tab] and loads the page "
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    90"
  • Dòng 15-16 tạo nút HTML. Clicking the button triggers the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    82 event handler, i. e. ,
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    87 được xác định trước đó

CỐ GẮNG. Include another button, which opens "

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

93"

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

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
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94. fires after browser loaded the page
  • 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. fires when the user points the mouse pointer at/away from the HTML element




  
  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 the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94 event handlers
  • In the opening tag [Line 12], we define the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94 event handler for the
    alert['hello, world']
    document.write['hello world, again']
    console.log['hello world, again and again']
    03 event. It invokes
    for [ initialization ; test ; post-processing ] {
       body ;
    }
    8 with the message defined earlier
  • Line 13 and 14 defines the event handlers
    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 for the HTML element

    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 property
    alert['hello, world']
    document.write['hello world, again']
    console.log['hello world, again and again']
    07 to an empty string]. The special keyword
    alert['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 says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

0
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

1
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

2Làm thế nào nó hoạt động?
  1. 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
  2. Let's begin with the HTML file. Now, the HTML file keeps only the contents, no presentation style and nor programming logic

    1. The CSS style sheet is kept in an external file, referenced via the above
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6 tag
      Note. In HTML4/XHTML1. 0 you need to include attribute
      alert['hello, world']
      document.write['hello world, again']
      console.log['hello world, again and again']
      11 in the
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6 opening tag

    2. Also, the JavaScript programming code is kept in an external file, referenced via the above
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6 is needed, although there is no content
      Note. In HTML4/XHTML1. 0, you need to include attribute
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      7 in the
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      3How it works?
      1. 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, two

        elements, three

        elements with

        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        17, and three
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        6 with unique
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        19

      2. In the user-defined function
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        20, we use
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        21 to select the element, and then modify its
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        22 property
      3. In
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        23 function, we use
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        24 to select all the

        elements 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. The
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        26 property keeps the length of the array

      4. In
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        27 function, we use
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        28 to select all the

        yế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 safest
        if [ condition-1 ] {
           block-1 ;
        } else if [ condition-2 ] {
           block-2 ;
        } else if [ condition-3 ] {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        8-variable, as
        alert['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 traditional
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        32 does not

      5. The page contains three buttons to trigger the functions defined
      6. 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
      7. 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 the
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94 handler via
        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. The
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        33 function assigns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 event handlers to the buttons, selected via
        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
      8. Instead of
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        22 property, older versions of JavaScript use
        alert['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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      4
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      5How it works?
      1. The HTML page has an hyperlink
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        6with an unique
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        19
      2. The
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        33 function assigns an
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 handler to the hyperlink, selected via
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        39
      3. When the hyperlink is clicked, the
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 handler
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        50 triggered. If
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        16 returns true, the new page will be loaded; otherwise, the current page remains

      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 data

      After 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 Tools

      In Chrome and Firefox, you can press F12 to activate the developer Tools [hence called F12 Developer Tools]

      It provides these features

      1. Console. View the JavaScript error messages. Start the firebug and switch to the "Console" panel
      2. 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.
      3. DOM. Xem DOM HTML của tài liệu hiện tại
      4. HTML và CSS

      Để gỡ lỗi JavaScript

      1. 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
      2. 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
      3. Để 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. ]
      4. 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;
      5. 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']
      53

      Thay 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ụng
      alert['hello, world']
      document.write['hello world, again']
      console.log['hello world, again and again']
      56, viết
      alert['hello, world']
      document.write['hello world, again']
      console.log['hello world, again and again']
      57 và giá trị của
      alert['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ạn

      Ví 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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      6

      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

      1. JavaScript đính kèm trong
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        6 chạy trong khi tải trang
      2. 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82] hoặc tải trang [
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94]

      Do đó, có hai nơi để đặt JavaScript của bạn

      1. giữa các thẻ chứa
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        6;
      2. 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        95,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94], e. g. ,
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        66
      Phần tử
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6 chứa các câu lệnh lập trình JavaScript. Ví dụ,

      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
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      6 là cần thiết mặc dù không có nội dung. ?

      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ẻ
      
      
      
      
        
        JavaScript Example: User-defined function and  title Event Handler
        
      
      
        

      Example on event and user-defined function

      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ách

    3. JS độc lập Ví dụ 2. [LÀM]

      [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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ằng
      alert['hello, world']
      document.write['hello world, again']
      console.log['hello world, again and again']
      73 và kéo dài đến
      alert['hello, world']
      document.write['hello world, again']
      console.log['hello world, again and again']
      74

      lưu ý rằng

      • 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òng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        49
      • 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        49, như Java/C/C++/C#

      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ọn

      Biến, chữ và kiểu

      Khai báo biến [
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29,
      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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29 để khai báo biến toàn cục. ES6 giới thiệu hai từ khóa mới.
      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ánh
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29

      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 danh

      Mã đị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ã đị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
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        0 KHÔNG phải là một
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        2, và KHÔNG phải là một
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        1
      • 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 [
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        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 [
        alert['hello, world']
        document.write['hello world, again']
        console.log['hello world, again and again']
        95] thay vì dấu gạch nối [
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        01]
      chữ

      Một chữ là một giá trị cố định, e. g. ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ức

      các loại

      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

      1. 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. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        08,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        13]
      2. số. lấy cả hai số nguyên [e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        06] hoặc dấu phẩy động [e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        15]
      3. boolean. lấy nghĩa đen boolean của một trong hai
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 hoặc
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27 [viết thường]
      4. chưa xác định. nhận một giá trị theo nghĩa đen đặc biệt được gọi là
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        18. Lưu ý rằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        18 vừa là loại vừa là giá trị theo nghĩa đen
      5. symbol [ES6]. Một kiểu dữ liệu có thể hiện là duy nhất và không thay đổi
      6. bigint [ES2020/ES11]. số nguyên với độ chính xác tù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]

      1. mục tiêu. cho các đối tượng chung
      2. 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
      3. 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]
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        20. Hãy lưu ý rằng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 KHÔNG phải là
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        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.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        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.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 thực sự được coi là nguyên thủy vì nó không có thuộc tính và phương thức

      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ư

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ểu
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ểu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ến

      Người điều hành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      31

      Bạn có thể sử dụng toán tử

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ỗi

      Ví dụ,

      Loại
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      18 và giá trị theo nghĩa đen của
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      18

      Một biến không được khai báo [thông qua từ khóa

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29] có một loại đặc biệt gọi là
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      18. Bạn không thể tham khảo giá trị của nó

      Khi một biến được khai báo [thông qua từ khóa

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29] mà không gán giá trị ban đầu, nó sẽ có kiểu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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]

      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áo

      Bạ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 cho
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29]. Ví dụ,

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      7

      Loại
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74, Chữ & Hoạt động

      Một biến kiểu

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 chứa một số, có thể là số nguyên hoặc số dấu phẩy động

      Chữ số nguyên có thể được biểu thị bằng

      • Số thập phân. bắt đầu bằng một chữ số
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        47 đến
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        48 [không phải
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        51,
      • bát phân. bắt đầu bằng một chữ số
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        49, e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        53 hoặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        54,
      • thập lục phân. bắt đầu bằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        55 [hoặc
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        58
      • nhị phân. bắt đầu bằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        59 [hoặc
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript 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. ]

      Các chữ số dấu phẩy động có thể được biểu diễn ở dạng thông thường [e. g. ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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!

      65

      JavaScript cũng cung cấp một số

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 chữ đặc biệt

      • vô cực. e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        67
      • -vô cực. e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        68
      • NaN [Không phải là số]. e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        69 hoặc chuyển đổi chuỗi
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        70 thành một số
      Các phép tính toán học

      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

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      71,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      72 trước khi phẫu thuật

      OperatorDescriptionExampleResult [
      
      
      
      
        
        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++;
      Giống như. y = x;
      Giống như. x=x-1;
      [ES7]y ** x

      Trong 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à,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      75 cho ____76_______76 [thay vì 0 trong Java/C/C++]. Bạn có thể sử dụng hàm có sẵn
      
      
      
      
        
        JavaScript 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 cho
      
      
      
      
        
        JavaScript 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ên

      Toán Tử Số Mũ [
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      0]

      ECMAScript 2016 [ES7] giới thiệu toán tử số mũ [

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      0], bạn có thể viết
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      86, e. g. ,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      87

      Trước ES7, bạn phải sử dụng chức năng tích hợp sẵn

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      88

      Toán tử số học kiêm toán tử gán

      Đâ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
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      89 thường được sử dụng
      • parseInt[str], parseFloat[str]. Phân tích cú pháp
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        3 cho đến số không phải chữ số đầu tiên và trả về số;
      • 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
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        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: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        93,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        94
      • Số[str]. Trả về số được đại diện bởi
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        25 hoặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        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ểu
      • toFixed[decimalPlaces]. Trả lại số/chuỗi này về số vị trí thập phân đã cho. Ví dụ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30 Loại, Chữ & Hoạt động

      Một

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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: The Date object
        
      
      
      
      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ư
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      58 cho dòng mới,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      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]

      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

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ếu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30 chứa dấu nháy kép [e. g. ,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      11

      
      
      
      
        
        JavaScript 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 là kiểu động và tự động thực hiện chuyển đổi kiểu. Khi giá trị

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      00. Bổ sung hoặc nối?

      Nếu cả hai toán hạng của toán tử

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      00 là
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      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: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30, thì toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ành
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74.
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      77,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      29 và
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      30

      Để chuyển đổi một số

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30 thành một số
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74, bạn có thể sử dụng các hàm tích hợp sẵn
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      77 hoặc
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      29, hàm này sẽ trả về một số
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 nếu chuyển đổi thành công; . Ví dụ,

      Hãy lưu ý rằng

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      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ụ,

      Bạn cũng có thể sử dụng hàm

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      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ành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      42 and
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      43

      Bạn có thể xây dựng một

      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30 từ một loại khác thông qua hàm
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      42, e. g. ,

      Bạn có thể sử dụng

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      43 để có được một đại diện
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      30 của một đối tượng, e. g. ,

      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ỗi
      • chiều dài. e. g. ,
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        48 trả về độ dài của chuỗi
      Hoạt động của chuỗi
      • Đế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í
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        49. Index begins from 0. Negative index can be used, which counts from the end of the string
      • substring[beginIdx, endIdx]. returns the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        50 [inclusive] to
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        51 [exclusive]
      • substr[beginIdx, length]. returns the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        50 of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        53
      • indexOf[searchStr, fromIdx?]. Return the beginning index of the first occurrence of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        54, starting from an optional
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        55 [default of 0]; or
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        56 if not found
      • lastIndexOf[searchStr, fromIdx?]. Return the beginning index of the last occurrence of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        54
        
        
        
        
          
          JavaScript Example: User-defined function and  title Event Handler
          
        
        
          

        Example on event and user-defined function

        6, starting from an optional
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        55 [default of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        60]; or
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        56 if not found
      • slice[beginIdx, endIdx]. Return the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        62 [inclusive] to
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        63 [exclusive]
      • repeat[count] [ES6]. repeat the string
      • split[delimiter], array. join[delimiter]. returns an array by splitting the string using
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        64, e. g. ,
      • Searching/Modifying Strings using Regular Expression. See ""
      Character Type?

      JavaScript 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

      • charAt[idx]. For example,
      • Array Bracket Operator []. You can treat a string as a character array, and access individual character via array bracket operator
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        65, e. g. ,

      [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. ,

      
      
      
      
        
        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: The Date object
        
      
      
      
      01,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      02]. Escape sequences are to be used for special characters [e. g. ,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      58 for new-line,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      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 supported

      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. ,

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      75

      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
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      76

      Template strings support substitution, which allow you to embed any valid JavaScript expression as part of the string in the form of

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      76, for example,

      Multi-line template string with substitution provides a more convenient way to write out formatted HTML fragment. For example,

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      8

      The
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 Type, Literals & Operations

      A variable of the type

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 holds a value of either
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 of
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27.
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 and
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27 are keywords in JavaScript

      As mentioned, JavaScript performs automatic type conversion if necessary. During the type conversion, the following 6 values are converted to

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27

      1. number
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        49
      2. number
        
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        91 [Not-a-Number],
      3. empty string [
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        13,
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        88],
      4. 
        
        
        
          
          JavaScript Example: Variables and functions prompt[] and confirm[]
          
        
        
          

        Welcome to JavaScript!

        18 value,
      5. boolean
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27,
      6. My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 [unallocated] object

      All the other values are converted to

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26

      You need to memorize this.

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      93 return
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 if
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      95 is one of these 6 values.
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      49,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      91,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      13,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt[] and confirm[]
        
      
      
        

      Welcome to JavaScript!

      18,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27, and
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21

      Comparison [Relational] Operators

      The following relational operators, which produce a

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 result, are defined in JavaScript. The results are obtained assuming
      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

      OperatorDescriptionExampleResult
      [
      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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      9
      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']
      1
      alert['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

Chủ Đề