Example JavaScript program
<script type="text/javascript">
document.writeln("Hello, <b>JavaScript</b>!");
</script>
- Browser will execute the output statement and display "Hello JavaScript!"
<script>
tags can be placed in the <head>
or <body>
type
attribute defaults to text/javascript
and
can be omitted
document
object represents the document (web page) being displayed
writeln()
method outputs HTML to the document
- Semicolons are typically used to terminate statements but are not always required (see
semicolon debate)
- JavaScript is case sensitive, so
Document.WriteLn()
is a syntax error
- Syntax errors will result in your script silently failing
- In Chrome, check the JavaScript Console (Ctrl-Shift-J) for
error messages
- JavaScript Console can also be used to execute JavaScript on-the-fly