Variables Kevin Harville
Variables Variables are a named space in the computer where a value is stored. Examples User’s age User’s name Any characters or numbers you need use in which the values may vary.
Variable Names Names are CASE SENSITIVE Var totalStudents is not TotalStudents Use letters or underscore. Can use numbers after the first character var Student_Name; var Score7; Be descriptive. Use no punctuation or spaces. var NumberOfFishInGrandmasAquarium;
Variable Scope The “scope” of a variable is the range within the program where it can be used. If a variable is declared within a function, the scope of the variable is local, meaning limited to that function. Other variables are global to the entire script.
Data Types Variables in JavaScript are variant. That means that they are usually not specified as being a particular type of data, such as characters, a decimal number, or a number. However, we can specify our data as being of a particular type if needed.
Data Types Numbers Boolean ( true / false ) Strings (characters) Null values (JavaScript keyword null)
Converting Strings to Numbers Any input from the user may be considered by the computer to be strings of characters. “10” + “40” = “1040” Therefore we tell the computer to consider the text to be integer or floating point (decimal numbers). age = parseInt(age); myFraction = parseFloat(myInput);
Just Remember… If your input should be an integer convert it using parseInt(). If your input should be a decimal convert it using parseFloat(). If your response is NaN, then the computer can’t convert it to a number