Download presentation
Presentation is loading. Please wait.
Published byLaureen Flynn Modified over 8 years ago
1
CS346 Javascript -3 Module 3 JavaScript Variables
2
CS346 Javascript -3 Variables Variable — value stored in computer memory location Represents a value that can be changed anytime Saves time in writing and updating scripts Makes the purpose of the code clearer Use the reserved keyword var to create variables Reserved words, or keywords, are part of the JavaScript syntax Cannot be used for variable names
3
CS346 Javascript -3 Variables Variables must begin with a letter, $, or _ Cannot contain any spaces Variables, like other JavaScript code, are case-sensitive You can declare multiple variables in a statement by separating them with commas var first=1, second=“two”, third=“three”;
4
CS346 Javascript -3 Variables Conventions Use a lowercase letter for the first letter of first word and uppercase for subsequent words myVariable Use _ to separate individual words my_variable
5
CS346 Javascript -3 Variable Scope Variable scope can be either global or local A global variable is declared outside of a function Available to all parts of the program A local variable is declared inside a function and is only available within the function Arguments within the parentheses of a function are considered local variables
6
CS346 Javascript -3 Data Types String eval( ) myValue = eval(“1+2”); parseInt( ) or parseFloat( ) Start = parseInt(document.form.start.value); Number( ) Start = Number(document.form.start.value); String( ) var num = 10; var numStr = String(num); isNaN( ) Num = isNaN(document.form.start.value); Integer 8 1,958 -16 Floating Point 8.16 5.0e13 Boolean True False Null
7
CS346 Javascript -3 Special Characters Escaping — Displaying special characters Use the backslash ‘\’ HTML Tags Enclosed in Quotes Escape Sequences \n = New Line \r = Carriage Return \t = Tab \’ = Single Quote \” = Double Quote \\ = Backslash
8
CS346 Javascript -3 Example 3-1variable.htm 3-1variable.htm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.