By the end of this page you will be able to:
- Ending Statements with a Semicolon?
- Semi-colons are used to separate pieces of code.
- This creates heaps of problems when you first start!!!
- White Space doesn't count!
- - but really useful to make your code readible.
- To Break a line of Code in the middle of a string use \, (Note next line has to start at column 1) for example:
- Alternatively you could concatenate (join) the string
- Inserting special characters (use \)
External Script (js1_03)
Example :Click here to run js1_03.
Rules for Variable names:
- Variable names are case sensitive
- They must begin with a letter or the underscore character
- Creating variables
- using var is optional (it is good practice to use var)
- When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables.
- If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
- If you redeclare a variable it loses its initial value.
- The prompt object is a way that user input can be obtained and assigned to a variable... well almost! Microsoft in Internet Explorer (from version 7) have disabled this functionality, when the prompt command is called from a page on the Internet (Hunlock, 2007).
A Prompt example
(Works in most Internet browsers. Does not work in Internet Explorer 7.x)
Example :Click here to run js1_04.
- Hunlock, P. (2007) Working around IE7s prompt bug, er feature. Retrieved April 3, 2009 from http://www.hunlock.com/blogs/Working_around_IE7s_prompt_bug,_er_feature