Page 329 - Webapplication11_C11_Flipbook
P. 329
4.11 COMMON ERRORS IN JAVASCRIPT
As you know, JavaScript is an interpreted language, errors in your code can cause it to behave unexpectedly or fail to
run correctly in the browser. These mistakes don't always produce detailed error messages, which can make debugging
challenging. Some common mistakes that can prevent your JavaScript code from displaying or functioning properly in
the browser are as follows:
Ð ÐMissing Quotation Marks: Strings in JavaScript must be enclosed in quotation marks ("" or ''), and missing one can
result in an error.
Ð ÐCase Sensitivity: JavaScript is case-sensitive, so commands like document must be written in lowercase. Writing
Document with a capital "D" will result in an error.
Ð ÐMissing Parentheses/Brackets: Functions like document.write() require parentheses to work. If you forget the
closing parenthesis, it will throw a syntax error.
Ð ÐMissing <script> Tag: JavaScript code in an HTML document must be enclosed within <script> tags. Forgetting either
the opening or closing <script> tag will prevent the code from running.
Ð ÐMisspelled Variable Names: JavaScript variable names are also case-sensitive, so if you declare a variable but later
refer to it with a different case or misspelled name, it will cause an error.
Ð ÐMissing Semicolons: While JavaScript can often infer semicolons, omitting them can sometimes cause unexpected
behaviour, especially in complex statements or when chaining multiple statements on the same line.
Ð ÐUnclosed Comments: Forgetting to close a comment can cause large sections of your code to be ignored by the
browser.
Ð ÐUsing Undefined Variables: Referencing a variable before it is defined or misspelling a variable name can cause
undefined errors or other unexpected results.
4.12 THE WRITE() AND WRITELN() METHODS
Both methods are used to display output on the web page. The write() method writes straight to an open (HTML)
document stream (A stream an object that allows to read/write data). The writeln() method also writes directly
to an open HTML document stream, with the addition of writing a newline character after each statement. For
example:
The following output will be in a single line.
document.writeln("I love reading Harry Potter books");
document.writeln("I also love reading murder mysteries");
The following output will be in two lines.
document.writeln("I love reading Harry Potter books");
document.writeln("I also love reading murder mysteries");
Let us create a JavaScript program:
<HTML>
<HEAD>
<TITLE> Using write() and writeln() Methods </TITLE>
</HEAD>
<BODY text="blue">
<H2> The write() and writeln() Methods </H2>
<PRE>
<SCRIPT TYPE="text/javascript">
document.write("Hello!");
JavaScript Part 1 327

