NJIT CS 103 - Fundamental Concepts Expressed in JavaScript

Unformatted text preview:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyFluency with Information TechnologyThird Editionby Lawrence SnyderChapter 18: Get With the Program:Fundamental Concepts Expressed in JavaScript18-21-2Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyOverview: Programming Concepts• Programming: Act of formulating an algorithm or program• Basic concepts have been developed over last 50 years to simplify common programming tasks• Concepts will be expressed in JavaScript18-31-3Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyProgramming Concepts• Names, values, variables• Declarations• Data types, numbers, string literals and Booleans• Assignment• Expressions• Conditionals18-41-4Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley18-51-5Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley18-61-6Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyNames, Values, And Variables• Names Have Changing Values– Example: U.S. President has current value of George W. Bush, previous values of Bill Clinton, George Washington• Names in a Program Are Called Variables– Values associated with a name change in programs using the assignment statement ( = )18-71-7Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyIdentifiers and Their Rules• Identifier is the character sequence that makes up a variable's name– Must have a particular form• Must begin with a letter or underscore ( _ ) followed by any sequence of letters, digits, or underscore characters• Cannot contain spaces• Case sensitive (Capitalization matters!)18-81-8Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyIdentifiers and Their RulesValid InvalidfirstOne 1stOnefirst1 first-1first_1 first$1first_One first OneFirstOne First1!18-91-9Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyA Variable Declaration Statement• Declaration: State what variables will be used– Command is the word var– For example, a program to calculate area of circle given radius, needs variables area and radius:• var radius, area;• The declaration is a type of statement18-101-10Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThe Statement Terminator• A program is a list of statements• The statements may be run together on a line• Each statement is terminated by the statement terminator symbol– In JavaScript, it is the semicolon ( ; )18-111-11Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyRules for Declaring Variables• Every variable used in a program must be declared (before it is used)– In JavaScript declaration can be anywhere in the program– Programmers prefer to place them first• Undefined values– Variable has been declared but does not yet have a valuevar number1; // undefined valuevar number2 = 42; // initialized to the value 4218-121-12Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyInitializing a Declaration• We can set an initial value as part of declaration statement:– var taxRate = .088;• Related variables may be grouped in one declaration/initialization; unrelated variables are usually placed in separate statementsvar num1 = 42, num2, num3; var num1 = 42;var num2;var num3;18-131-13Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThree Basic Date Types of Javascript• Numbers• Strings• Booleans– These kind of values are called data types or just types18-141-14Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyNumbers• Rules for Writing Numbers– There are no "units" or commas– Can have about 10 significant digits and can range from 10-324to 1030818-151-15Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyStrings• Strings are sequences of keyboard characters• Strings are always surrounded by single ( ' ' ) or double quotes ( " " )• Strings can initialize a declaration– var hairColor = "black";18-161-16Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyRules for Writing Strings in JavaScript• Must be surrounded by single or double quotes• Allow most characters except return (Enter), backspace, tab, \• Double quoted strings can contain single quoted strings and vice versa• The apostrophe ( ' ) is the same as the single quote• Any number of characters allowed in a string• Minimum number of characters is zero ( "" ), which is the empty string18-171-17Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyLiterals• String Literals stored in the computer– Quotes are removed (they are only used to delimit the string literal)– Any character can be stored in memory• Even a character that cannot be typed can be stored, using escape mechanism – in JavaScript, the backslash ( \ )18-181-18Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley18-191-19Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBoolean Values• Two logical values: True and False• They are values, not identifiers or strings• Used implicitly throughout programming process; only occasionally for initializing variables– Mostly used to compare data or make decisions18-201-20Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThe Assignment Statement• Changes a variable's value<variable> <assignment symbol> <expression>;• Assignment Symbol:– In JavaScript, the equal sign ( = )– Example:• weeks = days / 7;18-211-21Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley18-221-22Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyInterpreting an Assignment Statement• Value flows from the right side to the left side• Read the assignment symbol as "is assigned" or "becomes" or "gets"• The expression (right side) is computed or evaluated first– If there are any variables in it, their current value is used• Then this computed value becomes the value of the variable on the left side18-231-23Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyThree Key Points about Assignment• All three of the components must be given– if anything is missing, the statement is


View Full Document
Download Fundamental Concepts Expressed in JavaScript
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Fundamental Concepts Expressed in JavaScript and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Fundamental Concepts Expressed in JavaScript 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?