Whats a statement in javascript?

Moving through the eleven statements supported by JavaScript

Statements are used in JavaScript to control its program flow. Unlike properties, methods, and events, which are fundamentally tied to the object that owns them, statements are designed to work independently of any JavaScript object. That means you can use a statement whether you're working with the document object, the window object, the history object, or some other object. As a language, JavaScript supports relatively few statements -- just enough to get by. It does, however, offer the bare minimum of statements that are needed to construct a fully-functional application.

JavaScript currently supports the following eleven statements. Note that some of these -- such as comment, var, and new -- aren't bona fide statements, but they are often treated as such. They are included in this discussion for the sake of completeness.

  • // comment
  • break
  • continue
  • for
  • for...in
  • function
  • if...else
  • new
  • return
  • var
  • while
  • with

A few of the statements offered in Java are notably lacking in JavaScript. This includes the switch statement, as well as all forms of error-trapping statements [such as catch and throw]. JavaScript keeps these statements as reserved words, and perhaps in some future version, they will be implemented.

Comment [//]

The // characters tell JavaScript that you want to include explanatory comments in your program. The comment ends at the first hard return that is encountered. JavaScript places no limit on the length of the comment, as long as there is no hard return before the comment ends. JavaScript assumes text after the hard return is valid code.

// This is a simple comment

// This is another comment that spans more than one line. Though the comment wraps to the second line, the first line ends with a "soft return" in the text editing program. No hard return character is inserted.

You can place the // comment characters anywhere on a line. JavaScript will treat all the text on that line after the // as a comment.

MyVariable="This is a test" // assigns text variable MyVariable

Comments are ignored when the script is played, so they do not greatly affect the speed of execution. However, lots of comments increase the file size of scripts and take longer to download to the user's computer over a dial-up Internet connection. For best results, limit comments in JavaScript programs to brief, single lines.

When writing long comments it's better to use the alternate commenting characters /* and */. Text between these characters is treated as a comment. Alternatively, you can start each line with the // comment characters.

// This section checks to see if the Enter key is pressed, // then continues on

or

/* This section checks to see if the Enter key is pressed, then continues on */

Back to index

Break

The break statement tells JavaScript to exit a "controlled structure" and resume execution at a point after the structure. The break statement is used with structures built using the following commands:

  • for
  • for...in
  • while

The break statement is most commonly used to prematurely end a for loop. For example:

for [Count=1; Count

Chủ Đề