The value can be something that has to be calculated or something that will be

The value can be something that has to be calculated

This preview shows page 14 - 23 out of 41 pages.

The value can be something that has to be calculated, or something that will be provided later, like user input. Variable declared without a value will have the value undefined . The variable carname will have the value undefined after the execution of the following statement: var carname; 14
Image of page 14
Re-Declaring JavaScript Variables If you re-declare a JavaScript variable, it will not lose its value:. The value of the variable carname will still have the value " Volvo " after the execution of the following two statements: var carname="Volvo"; var carname; 15
Image of page 15
Variable Scope A variable declared (using var) within a JavaScript function becomes LOCAL and can only be accessed from within that function. (the variable has local scope). You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared. Local variables are deleted as soon as the function is completed. Variables declared outside a function, become GLOBAL , and all scripts and functions on the web page can access it. 16
Image of page 16
The Lifetime of JavaScript Variables The lifetime of JavaScript variables starts when they are declared. Local variables are deleted when the function is completed. Global variables are deleted when you close the page If you assign a value to a variable that has not yet been declared, the variable will automatically be declared as a GLOBAL variable. This statement: carname="Volvo“; will declare the variable carname as a global variable , even if it is executed inside a function. 17
Image of page 17
JavaScript Arithmetic Given that y = 5 , the table below explains the arithmetic operators: Arithmetic operators are used to perform arithmetic between variables and/or values 18
Image of page 18
JavaScript Data Types String, Number, Boolean, Array, Object, Null, Undefined. JavaScript has dynamic types. This means that the same variable can be used as different types: Example var x; // Now x is undefined var x = 5; // Now x is a Number var x = "John"; // Now x is a String 19
Image of page 19
Addition 1. <!DOCTYPE html> 2. <html> 3. <body> 4. <p>y = 5, calculate x = y + 2, and display x:</p> 5. <button onclick="myFunction()">Click Me</button> 6. <p id="demo"></p> 7. <script> 8. function myFunction() { 9. var y = 5; 10. var x = y + 2; 11. document.getElementById("demo").innerHTML = x; 12. } 13. </script> 14. </body> 15. </html> 20
Image of page 20
Subtraction <!DOCTYPE html> <html> <body> <p>y = 5, calculate x = y - 2, and display x:</p> <button onclick="myFunction()">Click Me</button> <p id="demo"></p> <script> function myFunction() { var y = 5; var x = y - 2; document.getElementById("demo").innerHTML = x; } </script> </body> </html> 21
Image of page 21
JavaScript Strings A string is a variable which stores a series of characters like "John Doe". A string can be any text inside quotes.
Image of page 22
Image of page 23

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture