170 likes | 271 Views
JavaScript Assignment Statements Expressions G lobal Functions. CST 200 JavaScript. Objectives. Introduce JavaScript Assignment Statements and Expressions Introduce Global Properties and Functions. Assignment Statements.
E N D
JavaScript Assignment StatementsExpressionsGlobal Functions CST 200 JavaScript
Objectives • Introduce JavaScript Assignment Statements and Expressions • Introduce Global Properties and Functions
Assignment Statements • In previous examples, we have used assignment statements to assign values to variables var user = "Joe Smith"; Declare variable named user Assign the string value Joe Smith to the variable user
Assignment Statements (cont) • Assignment statements are used to assign (set) a value to a variable • Assignment statements use the assignment operator ( = ) • The right-side of the assignment statement contains an expression var size = 100 + ( 20 * 3 ); • The syntax (rule) for an assignment statement is: variable name = expression Expression
Expressions • An expression is a valid unit of code that evaluates to a value • Typically takes the form • Value var message = "hello"; • Arithmetic Expression var total = 20 + 34; • String Expression var name = "Bill" + "Smith"; • Function call var age = prompt("Enter age:");
Expressions (cont) • An expression is a valid unit of code that evaluates to a value • An expression is always evaluated and the result is assigned to the variable var x = 20 + ( 0.5 * 18 ); var x = 29;
Expressions (cont) • Variable names used in an expression are replaced with the current value of the variable var length = 5; var width = 20; var perimeter = 2 * ( length + width ); var perimeter = 2 * ( 5 + 20 ); var perimeter = 50; The expression on the right-side of the assignment statement is always evaluated first
Expressions (cont) • The same variable can be used on both sides of an assignment statement var x = 12; x = x+ 15; x = 12 + 15; x = 27; The expression on the right-side of the assignment is evaluated first When a variable name is used in an expression, the current value of the variable is used
Expressions (cont) • An expression is always evaluated and the result is assigned to the variable var user = "Susan"; var name = "Hello " + user; var name = "Hello Susan"; When a variable name is used in an expression, the current value of the variable is used
Expressions (cont) • When the expression is a method (function), the result of the function is assigned to the variable var color = window.prompt("Enter favorite color:"); var color = "peach"; The result of the window.prompt() method will be returned and assigned to the variable
Expressions Exercise • Given the variable assignments, and expression below, identify the result of the expression that will be assigned to the variable:
Global Properties and Functions • The JavaScript language defines global properties and functions that can be used in JavaScript programs • global refers to the scope, or places within the code where the properties and functions can be used • global scope means the properties and functions can be used anywhere
parseInt() Global Function • A common task in JavaScript programs is converting a string value into a numeric value • This is needed when we prompt the user for a numeric value through the window.prompt() method • window.prompt() returns a string value • Use parseInt() function to convert a string value into a numericinteger value
parseInt() Global Function (cont) • Examples: parseInt( 62 ) // returns 62 parseInt( 85.33) // returns 85 parseInt( "100 days of summer" ) // returns 100 parseInt( "7") // returns 7
parseInt() Global Function (cont) • We will use the parseInt() function along with the window.prompt() method • Specifically, we will pass the result of the window.prompt() method to the parseInt() function var x = parseInt(window.prompt("Enter rating [1-5]:")); 1 2 In evaluating this expression, the innermost function is evaluated first
parseInt() Global Function (cont) var x = parseInt(window.prompt("Enter rating [1-5]:")); var x = parseInt( "4" ); var x = 4
parseFloat() Global Function • parseFloat() is used to convert a string value into a numeric floating point (real) number • Also will be used in conjunction with the window.prompt() method Examples: parseFloat( 1.25) // returns 1.25 parseFloat( "5.009") // returns 5.009 parseFloat( ".52ab" ) // returns 0.52