230NLn2.pptx - Basic Javascript Instructions 1 Statements A...

This preview shows page 1 - 7 out of 34 pages.

Basic Javascript Instructions 1
Statements A script is a series of instructions that a computer can follow one-by-one. Each individual instruction or step is known as a statement. Statements should end with a semicolon. JAVASCRIPT IS CASE SENSITIVE JavaScript is case sensitive so hourNow means something different to HourNow or HOURNOW. Each of the lines of code in green is a statement. • The pink curly braces indicate the start and end of a code block. (Each code block could contain many more statements.) • The code in purple determines which code should run var today= new Date{); var hourNow = today.getHours{) ; var greeting; if (hourNow > 18) { greeting= 'Good evening'; } else if (hourNow > 12) { greeting= 'Good afternoon'; } else if (hourNow > O) { greeting 'Good morning'; } else { greeting 'Welcome'; } document.write(greeting) ; 2
Statements A statement is an individual instruction that the computer should follow. Each one can start on a new line and end with a semicolon. This makes your code easier to read and follow. The semicolon also tells the JavaScript interpreter when a step is over, indicating that it should move to the next step. STATEMENTS CAN BE ORGANIZED INTO CODE BLOCKS Some statements are surrounded by curly braces; these are known as code blocks. The closing curly brace is not followed by a semicolon . Code blocks will often be used to group together many more statements. This helps programmers organize their code and makes it more readable. 3
Comments You should write comments to explain what your code does. They help make your code easier to read and understand. This can help you and others who read your code or remember what was the purpose of that code. MULTI-LINE COMMENTS To write a comment that stretches over more than one line, you use a multi-line comment, starting with the /* characters and ending with the * / characters. Anything between these characters is not processed by the JavaScript interpreter. Multi-line comments are often used for descriptions of how the script works, or to prevent a section of the script from running when testing it. SINGLE-LINE COMMENTS In a single-line comment, anything that follows the two forward slash characters // on that line will not be processed by the JavaScript interpreter. Single-line comments are often used for short descriptions of what the code is doing. Good use of comments will help you if you come back to your code after several days or months. They also help those who are new to your code. 4
Comments /* This script displays a greeting to the user based upon the current time. It is an example from JavaScript & jQuery book */ var today= new Date(); // Create a new date object var hour Now = today.getHours(); // Find the current hour var greeting; // Display the appropriate greeting based on the current time if (hourNow > 18) { greeting = 'Good evening ' ;} else if (hourNow > 12) { greeting = 'Good afternoon';} else if (hourNow > 0) { greeting= ' Good morning';} else { greeting = 'Welcome';} } document.write(greeting) ; 5
WHAT IS A VARIABLE?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture