JavaScript is a powerful but easy-to-learn programming language. Programs (also called “scripts”) written in JavaScript can be incorporated into Web pages or stored in a text file. There is no need to run the code through a compiler before using it.
Among the page features often created with JavaScript are:
- current date or time
- dialog boxes
- “dynamic” content that depends on user choices
- images that respond to the mouse
Because it is a client-side technology (i.e., it runs in the user’s browser), JavaScript does not require that a web developer have access to the web server. However, allowances should be made for users who do not have JavaScript turned on.
Syntax
Unlike HTML, JavaScript is case-sensitive: myWindow
is not the same as mywindow
.
JavaScript is less picky about line breaks and white space. You can break
lines almost anywhere, even in the middle of a statement. Each statement should
end with a semicolon (;) but JavaScript is not even particular about that.
Blocks of code, such as a function or an IF
statement, are often
enclosed in brackets {}. Parentheses () are used in many contexts, while square
brackets [] are used with arrays. It is important to always use brackets,
parentheses, and square brackets in pairs. This is one of the most common
syntax errors.
Another common error is to leave out a quotation mark. Quote marks are used to enclose literal text strings. Either single or double quotes may be used, but they have to match.
Developers commonly expect to spend twice as much time debugging their code as they spent writing it in the first place. This is the case with any programming language.
The SCRIPT
tag
JavaScript:The SCRIPT container
JavaScript statements are enclosed within SCRIPT
container tags. HTML comment containers are included in order to prevent
older browsers from displaying the JavaScript code on the page. The last
line is preceded by the JavaScript comment escaper: //
<SCRIPT TYPE="text/javascript">
<!--
.. JavaScript statements go here ..
//
-->
</SCRIPT>
JavaScript statements and variables can also be included in HTML tags by
using the event handler attributes or other core attributes such
as ID
and NAME
.
Data types
The four basic types of JavaScript are numbers, strings, booleans, and the value null.
Numbers can be integers (whole numbers, either positive or negative) or floating-point numbers (having decimals or fractions). They can be decimal (base-10), hexadecimal (base-16), or octal (base-8).
A string is a series of text characters such as a word,
a sentence, or today’s date. Strings are always enclosed in quote marks.
A string may contain zero characters; for example: var myString = "";
A boolean contains the value true or false.
Finally, null represents an empty value, containing nothing (not even a zero or a string with zero characters).
History
JavaScript was developed by Netscape in 1993 for the Netscape Navigator 2.0 browser. It was originally called LiveScript, but that was before Netscape entered into a business alliance with Sun Microsystems, which was promoting Java as an advanced programming language for the Internet. The partners decided it would be a good idea to name Netscape’s new technology “JavaScript” in order to create more buzz for Java. But the main result was to confuse millions of World Wide Web users right down to the present day.
The easy-to-learn language was popular with website developers, if not always with their visitors. Some early scripting efforts were more annoying than helpful to the user. But today JavaScript is almost taken for granted; an estimated 80 percent of users browse the Web with JavaScript enabled.
As Microsoft developed its Internet Explorer browser, it included JavaScript under the name JScript. Another implementation of the language is called InScript. The European Computer Manufacturers Association, seeking to prevent the development of two or three competing versions of this language, published an open standard called ECMA Script. For most developers, the differences between these languages are negligible, and for now the name “JavaScript” works in almost all contexts.