1 / 17

JavaScript Assignment Statements Expressions G lobal Functions

JavaScript Assignment Statements Expressions G lobal Functions. CST 200 JavaScript. Objectives. Introduce JavaScript Assignment Statements and Expressions Introduce Global Properties and Functions. Assignment Statements.

akasma
Download Presentation

JavaScript Assignment Statements Expressions G lobal Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript Assignment StatementsExpressionsGlobal 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 • Value var message = "hello"; • Arithmetic Expression var total = 20 + 34; • String Expression var name = "Bill" + "Smith"; • Function call var 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:

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

  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

More Related