I invent nothing; I rediscover.” Rodin
A resource for web developers a code snippet library with step by step tutorials.

a resource for web developers...

       

Search Amazon

JavaScript: Basics plus - build on your JavaScript knowledge

 

This page introduces the basics of JavaScript code constructs with examples.

 

Previous Previous

 

JavaScript: Arrays Next Next


JavaScript code snippets listed by task and organised in a tutorial style each building on the previous example. New topics are introduced when required.


1

Use of character data (CDATA) section: a refinement on the comment notation for hiding script and used for shielding JavaScript code that contains special characters such as <, >, & and " from the browser without the need to use character or numeric entity reference values.

CDATA sections: a requirement for XML and therefore XHTML.

Problems with most browsers and current compromise solution.

 

Note: placing your functions in an external .js function library file will remove the need to include the CDATA section, see JavaScript: Basics code snippet 3.

2.

Performing a Boolean condition test: true or false.

Simple test on (2<3) and (2>3).

Use of the 'valueOf()' method to return the true or false value.

3.

Selection: based on a true or false condition.

Building an 'if else' conditional with 'NOT' operator '!'.

Use of equality operator '=='.

Function calling another function.

4.

Building an 'if else' conditional with 'OR' comparison operator '||'.

Acting on user input from 'window.prompt()'.

4a.

Shortcut notation 'if else' conditional.

Introducing the shortcut notation for the 'if else' conditional.

Sometimes known as a ternary operator with the syntax '?:', a conditional expression equivalent to an 'if then else' and incorporated in a ternary statement:

condition ? if true : if false

5.

Building a nested 'if else' conditional.

Acting on user input from 'window.prompt()'.

6.

Building a multiple 'if' conditional with 'AND' comparison operator '&&'.

Acting on user input from 'window.prompt()'.

Same output as snippet 5 above but layout of code clearer.

7.

Repetition: building a simple 'while loop'.

Incorporating XHTML elements into output. Example: '<br />'.

8.

Repetition: building a simple 'for loop'.

Ideal if the loop body is to be repeated a known number of times.

9.

Use of 'switch' statement structure with 'case' and 'break'.

Including a default 'case' if none of the statements match.

10.

Build on the above and learn about the JavaScript Array object.

View code examples on JavaScript: Arrays page.

Suggest a code snippet to add to this category.

Rate this page:








Back to top.

 

Bookmark and Share

 

Copyright © 2006-2013 justfigures.co.uk