What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight programming language A JavaScript consists of lines of executable computer code A JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license {draw:rect} Are Java and JavaScript the Same? NO!
Java and JavaScript are two completely different languages in both concept and design! Java (developed by Sun Microsystems) is a powerful and much more complex programming language – in the same category as C and C++. {draw:rect} What can a JavaScript Do? JavaScript gives HTML designers a programming tool – HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small “snippets” of code into their HTML pages JavaScript can put dynamic text into an HTML page – A JavaScript statement like this: document. rite(“” + name + “”) can write a variable text into an HTML page JavaScript can react to events – A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element JavaScript can read and write HTML elements – A JavaScript can read and change the content of an HTML element JavaScript can be used to validate data – A JavaScript can be used to validate form data before it is submitted to a server.
This saves the server from extra processing JavaScript can be used to detect the visitor’s browser A JavaScript can be used to detect the visitor’s browser, and – depending on the browser – load another page specifically designed for that browser JavaScript can be used to create cookies – A JavaScript can be used to store and retrieve information on the visitor’s computer {draw:rect} The Real Name is ECMAScript JavaScript’s official name is “ECMAScript”.
The standard is developed and maintained by the ECMA organisation. ECMA-262 is the official JavaScript standard. The standard is based on JavaScript (Netscape) and JScript (Microsoft). The language was invented by Brendan Eich at Netscape (with Navigator 2. 0), and has appeared in all Netscape and Microsoft browsers since 1996. The development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997. The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998.
The development of the standard is still in progress. JAVASCRIPT OPERATORS The operator = is used to assign values. The operator + is used to add values. {draw:rect} The assignment operator = is used to assign values to JavaScript variables. The arithmetic operator + is used to add values together. The value of x, after the execution of the statements above is 7. {draw:rect} JavaScript Arithmetic Operators Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y=5, the table below explains the arithmetic operators: {draw:rect} JavaScript Assignment Operators Assignment operators are used to assign values to JavaScript variables. Given that x=10 and y=5, the table below explains the assignment operators: {draw:rect} The + Operator Used on Strings The + operator can also be used to add string variables or text values together. To add two or more string variables together, use the + operator. After the execution of the statements above, the variable txt3 contains “What a verynice day”.
To add a space between the two strings, insert a space into one of the strings: or insert a space into the expression: After the execution of the statements above, the variable txt3 contains: “What a very nice day” {draw:rect} Adding Strings and Numbers Look at these examples: Try it yourself. The rule is: If you add a number and a string, the result will be a string. JavaScript Comparison and Logical Operators {draw:a} {draw:a} {draw:rect} Comparison and Logical operators are used to test for true or false. draw:rect} Comparison Operators Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that x=5, the table below explains the comparison operators: {draw:rect} How Can it be Used Comparison operators can be used in conditional statements to compare values and take action depending on the result: You will learn more about the use of conditional statements in the next chapter of this tutorial. draw:rect} Logical Operators Logical operators are used in determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators: {draw:rect} Conditional Operator JavaScript also contains a conditional operator that assigns a value to a variable based on some condition. Syntax Example If the variable visitor has the value of “PRES”, then the variable greeting will be assigned the value “Dear President ” else it will be assigned “Dear”. JavaScript If…
Else Statements {draw:a} {draw:a} {draw:rect} Conditional statements in JavaScript are used to perform different actions based on different conditions. {draw:rect} Examples If statement How to write an if statement. If… else statement How to write an if… else statement. If.. else if… else statement How to write an if.. else if… else statement. Random link This example demonstrates a link, when you click on the link it will take you to W3Schools. com OR to RefsnesData. no. There is a 50% chance for each of them. draw:rect} Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements: if statement – use this statement if you want to execute some code only if a specified condition is true if… else statement – use this statement if you want to execute some code if the condition is true and another code if the condition is false if… else if…. lse statement – use this statement if you want to select one of many blocks of code to be executed switch statement – use this statement if you want to select one of many blocks of code to be executed {draw:rect} If Statement You should use the if statement if you want to execute some code only if a specified condition is true. Syntax Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error! Example 1 Example 2 Note: When comparing variables you must always use two equals signs next to each other (==)! Notice that there is no .. else.. in this syntax.
You just tell the code to execute some code only if the specified condition is true. {draw:rect} If… else Statement If you want to execute some code if a condition is true and another code if the condition is not true, use the if…. else statement. Syntax Example {draw:rect} If… else if… else Statement You should use the if…. else if… else statement if you want to select one of many sets of lines to execute. Syntax Example JavaScript Switch Statement {draw:a} {draw:a} {draw:rect} Conditional statements in JavaScript are used to perform different actions based on different conditions. draw:rect} Examples Switch statement How to write a switch statement. {draw:rect} The JavaScript Switch Statement You should use the switch statement if you want to select one of many blocks of code to be executed. Syntax This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
Example JavaScript Popup Boxes {draw:a} {draw:a} {draw:rect} In JavaScript we can create three kinds of popup boxes: Alert box, Confirm box, and Prompt box. {draw:rect} Examples Alert box { alert(“I am an alert box!! “); } Alert box with line breaks { alert(“Hello again! This is how we” + ‘
‘ + “add line breaks to an alert box! “); } Confirm box { var r=confirm(“Press a button”); if (r==true) { } else { } } Prompt box { var name=prompt(“Please enter your name”,”Harry Potter”); if (name! =null && name! “”) { } } {draw:rect} Alert Box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click “OK” to proceed. Syntax: {draw:rect} Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” or “Cancel” to proceed. If the user clicks “OK”, the box returns true. If the user clicks “Cancel”, the box returns false. Syntax: {draw:rect} Prompt Box A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks “OK” the box returns the input value. If the user clicks “Cancel” the box returns null. Syntax: JavaScript Functions {draw:a} {draw:a} {draw:rect} A function is a reusable code-block that will be executed by an event, or when the function is called. {draw:rect} Examples Function How to call a function. { alert(“HELLO”); } By pressing the button, a function will be called. The function will alert a message.
Function with arguments How to pass a variable to a function, and use the variable in the function. Function with arguments 2 How to pass variables to a function, and use these variables in the function. Function that returns a value How to let the function return a value. A function with arguments, that returns a value How to let the function find the product of two arguments and return the result. {draw:rect} JavaScript Functions To keep the browser from executing a script when the page loads, you can put your script into a function.
A function contains code that will be executed by an event or by a call to that function. You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external . js file). Functions can be defined both in the and in the section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the section. Example If the line: alert(“Hello world!! “) in the example above had not been put within a function, it would have been executed as soon as the line was loaded.
Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked. You will learn more about JavaScript events in the JS Events chapter. {draw:rect} How to Define a Function The syntax for creating a function is: var1, var2, etc are variables or values passed into the function. The { and the } defines the start and end of the function. Note: A function with no parameters must include the parentheses () after the function name: Note: Do not forget about the importance of capitals in JavaScript!
The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name. {draw:rect} The return Statement The return statement is used to specify the value that is returned from the function. So, functions that are going to return a value must use the return statement. Example The function below should return the product of two numbers (a and b): When you call the function above, you must pass along two parameters: The returned value from the prod() function is 6, and it will be stored in the variable called product. draw:rect} The Lifetime of JavaScript Variables 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. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared. 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. {draw:rect} {draw:a} {draw:a}