Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript.

Similar presentations


Presentation on theme: "JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript."— Presentation transcript:

1 JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript

2 Objectives Introduce JavaScript Assignment Statements and Expressions Introduce Global Properties and Functions

3 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

4 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

5 Expressions An expression is a valid unit of code that evaluates to a value Typically takes the form – Valuevar message = "hello"; – Arithmetic Expressionvar total = 20 + 34; – String Expressionvar name = "Bill" + "Smith"; – Function callvar age = prompt("Enter age:");

6 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;

7 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

8 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

9 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

10 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

11 Expressions Exercise Given the variable assignments, and expression below, identify the result of the expression that will be assigned to the variable: VARIABLEEXPRESSIONRESULT a = 10, b = 20a = a * b count = 100count = count + 20 a = "big", b = "cat"a = a + b

12 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

13 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 numeric integer value

14 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

15 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]:") ); In evaluating this expression, the innermost function is evaluated first 1 2

16 parseInt() Global Function (cont) var x = parseInt( window.prompt("Enter rating [1-5]:") ); var x = parseInt( "4" ); var x = 4

17 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


Download ppt "JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript."

Similar presentations


Ads by Google