Team LiB
Previous Section Next Section

Summary

The preceding chapter presented data types as the core of the language. This chapter showed how data types could be combined using operators to form expressions. JavaScript supports operators familiar to most programmers, including mathematical (+, , *, and %), bitwise (&, |, ^, <<<<, >>>>, and >>>>>>), comparison (<<, >>, ==, ===, !=, >>=, and <<), assignment (=, +=, and so on), and logical (&&, ||, and !). It also supports less common operators like the conditional operator (?:) and string concatenation operator (+). JavaScript operators are combined with variables and data literals to form expressions. Expressions must be carefully formed to reflect precedence of evaluation, and liberal application of parentheses will help avoid any problems. Statements can then be formed from expressions to make up the individual steps of a program. Individual statements are delimited in JavaScript using a semicolon or a return character. Semicolons should always be used to avoid ambiguity and improve script safety. The most common statements are assignment statements, functions, and method calls. These perform the basic tasks of most scripts. Control statements such as if and switch can alter program flow. A variety of loops can be formed using while, for, or do-while in order to iterate a particular piece of code. Further program flow control can be achieved with break and continue. As larger scripts are built using the constructs presented in this chapter, repetitive code is often introduced. To eliminate redundancy and create more modular programs, functions—the topic of the next chapter—should be employed.


Team LiB
Previous Section Next Section