Objectives and Skills
Objectives and skills for this lesson include:
- Comments; indentations; naming conventions; noscript; constants; reserved keywords; debugger keyword; setting breakpoints; console.log
- Declare and use variables of primitive data types
- Number; Boolean; String; Null; Undefined; typeof operator; type checking functions; use strict; converting between data types; formatting numbers; string operations; single quote vs double quote (nesting); initialization
- Complete or debug code that uses assignment and arithmetic operators
- Assignment; increment; decrement; addition; subtraction; division; multiplication; modulus; compound assignment operators
- YouTube: Conditionals Exercise Solutions
- YouTube: Math.random / Generate random numbers
- YouTube: indexOf() and lastIndexOf() String Methods
- YouTube: 20 String Methods in 7 Minutes
- Example Code
- Create a program that uses variables for hours and rate per hour and then calculate and displays weekly, monthly, and annual gross pay (hours * rate). Base monthly and annual calculations on 12 months per year and 52 weeks per year.
- Create a program that uses variables for years, and then calculate and displays an approximate age in months, days, hours, and seconds. For example, a person 1-year-old is 12 months old, 365 days old, etc.
- Review MathsIsFun: US Standard Lengths. Create a program that uses variables for a distance in miles, and then calculate and displays the distance in yards, feet, and inches, or calculate and display the distance in kilometers, meters, and centimeters.
- Review MathsIsFun: Area of Plane Shapes. Create a program that uses variables for the dimensions of different shapes and then calculate and displays the area of the shapes. Do not include shape choices. That will come later. For now, just include multiple shape calculations in sequence.
- Create a program that calculates the area of a room to determine the amount of floor covering required. The room is rectangular with the dimensions measured in feet with decimal fractions. The output needs to be in square yards. There are 3 linear feet to a yard.
- Review MathsIsFun: Order of Operations. Create a program that demonstrates the order of operations. Include parentheses, exponents, multiplication, division, addition, and subtraction in your program. Use variables for the calculations and label the output. For example, part of the program might display:
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
- Create a program that initializes different variables with integer, floating point, string, null, and undefined values. Demonstrate various operations and converting between data types. For example, user input is always a string, but adding string values of "1" + "1" is typically "11", whereas, adding numeric values of 1 + 1 is 2. Use
typeofto label each variable and the resulting output.
- Create a program that demonstrates string operations and string formatting. Define string constants or string literals that include apostrophes ('). Define string constants or string literals that include "quotes". Convert between string and numeric data types using Number() and .toFixed() with two decimal places. Concatenate string literals and variables to display output.
- Use the
debuggerstatement to create a breakpoint in one of the programs above. Experiment with single-stepping through the code and viewing script and global values. Add one or more variables to the watch window.
- Add a breakpoint to the program somewhere after the
debuggerstatement. After the program stops with the debugger, resume script execution and observe the script stopping at the breakpoint.
- Reserved words (for example, continue, debugger, break, etc.) can't serve as variable names.
- Names can begin only with a letter, an underscore or a dollar sign. Names are allowed to contain numbers, but a number can't be the first character.
- In Java Script names are case sensitive.
typeofoperator is used to find the data type of the variable.
- Strings are written using single or double quotation marks. Quotation marks can be used inside the string if they don't match the ones outside of it. Quotation marks used inside can match the outside ones only if they are preceded by the backslash escape character.
- "use strict" is a literal expression that requires / enforces variable declaration. It allows for cleaner code to be written.
- Assigning a value to a variable or constant with the "=" operator.
- Data type which has only two values, True and False.
- camel case
- Method of joining multiple words into one variable name where the first word is not capitalized and every subsequent word is capitalized.
- A container for storing a permanent data value that cannot be changed later in the program.
- A special value which represents "nothing." It is not a reference to a null pointer.
- A variable that can contain many values.
- Can consist of one character or many. A string must be surrounded by quotation marks.
- type coercion
- The automatic or implicit conversion of values from one data type to another (such as strings to numbers).
- A variable which has not been assigned a value. If a variable is simply declared, but not assigned anything, it becomes undefined.
- A container for storing a data values.
- Wikipedia: Variable (computer science)
- Wikipedia: Constant (computer programming)
- Wikipedia: Data type
- Wikipedia: Expression (computer science)
- Wikipedia: Statement (computer science)
- Wikipedia: Assignment (computer science)
- Wikipedia: Order of operations
- Wikipedia: Self-documenting code
- PythonLearn: Variables, expressions, and statements
- Wikibooks: Programming Fundamentals/Practice: Data and Operators
- MDN Web Docs: Data Types and Data Structures
- MDNː Type coercion