250 likes | 449 Vues
This overview of JavaScript variables provides foundational knowledge essential for aspiring programmers. It covers the lifecycle of variables—declaring, initializing, using, and destroying them—and delves into data types such as strings, numbers, booleans, arrays, and objects. You’ll learn about local and global variable declarations, basic operations like addition and subtraction, and essential user interface commands like `alert`, `prompt`, and `confirm`. Understand how to effectively manage your data with practical examples and gain insight into how JavaScript interacts with the DOM.
E N D
currentStudent Basics of a Variable • Defines a memory location. • Stores data. • Value can change as script runs. Jane
currentStudent Characteristics of a Variable • Name. • Data type. • Value. String Jane
Variable Lifecycle Declare Initialize Use Destroy
Data Type • Defines values and operations permitted. • Types: • String. • Number: integer, floating point (decimal), … • Boolean. • Array. • Object.
Names, Types, & Values A Number = 123.59 floating point ANumber = 123.59 string ANumber = “123.59” string ANumber = “Hello World!” string curStatus = “false” Boolean curStatus = false
Initialize Declare Use Declaring and Using a Variable <script type="text/javascript"> var keepTrack = 'Start' alert(keepTrack) keepTrack = '100' alert(keepTrack) </script>
local declaration out of scope Local Variable <script type="text/javascript"> function myFunction1() { var aVar = 1 alert('myFunction1: ' + aVar) } function myFunction2() { alert('myFunction2: ' + aVar) } </script>
global declaration Global Variable <script type="text/javascript"> var aVar = 1 function myFunction1() { alert('myFunction1: ' + aVar) } function myFunction2() { alert('myFunction2: ' + aVar) } </script>
Basic Operations • Addition (numbers and strings). • Subtraction. • Multiplication. • Division.
Some JavaScript User Interface Commands • alert(message) – no returned value. • prompt(prompt, default) • confirm(prompt) * window object methods.
Function/Method Returns Value variable = function(parameters) variable = object.method(parameters) variable = prompt(prompt,default) variable = confirm(prompt)
Examples grossPay = computeGross(hours, rate) userName = prompt(‘What is your name?’,’’) setAsHome = confirm(‘Set as home page?’)
? Properties Interface Methods Review of Objects
Using Objects object.property object.method() object.method(param1, param2, …) parentobject.childobject.method()
A Simple Accumulator Object Methods Properties increase(amount) currentValue decrease(amount) current()
Using the Accumulator Object currentValue Out accumulator.currentValue = 3 3 accumulator.increase(5) 8 accumulator.current() 8 accumulator.decrease(4) 4 accumulator.currentValue 4
JavaScript Document Object Model (DOM) Basics window document location screen history navigator
Reminder object properties methods Support varies by browser type and browser version. DOM references identify properties and methods.