Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?

Slides:



Advertisements
Similar presentations
1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Lesson 4: Formatting Input Data for Arithmetic
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
JavaScript, Fourth Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Input/Output and Variables Ethan Cerami New York
String Escape Sequences
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
2440: 211 Interactive Web Programming Expressions & Operators.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Input, Output, and Processing
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
The Grammar of JavaScript.  Each line of a script is a statement  An instruction that JavaScript can evaluate (make sense of)  Ends with a semicolon;
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
CS346 Javascript -3 Module 3 JavaScript Variables.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
1 homework Due today: hw #1 (mailing list printout) readings to date: chapter 1 and chapter read appendix B (3 pages on DOS) and and.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 2 Variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
Python Let’s get started!.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Variables.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
Project 4: Working with Variables Essentials for Design JavaScript Level One Michael Brooks.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
>> Introduction to JavaScript
Python Let’s get started!.
Documentation Need to have documentation in all programs
Variables and Primative Types
Scope, Objects, Strings, Numbers
JavaScript.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Variables Kevin Harville.
Tutorial 10: Programming with javascript
Presentation transcript:

Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it? How do we use it in code? 1

2 Data Types Common: Dates, Text, Numbers Other, more abstract, data types – boolean, etc. Strongly typed vs. weakly typed – JavaScript is weakly typed – Java is strongly typed

3 Numerical Data Integers - range from -2E53 to 2E53 Floating-point numbers (have a decimal place) JavaScript treats these different when storing them JavaScript processes everything as if it were a floating point Hides the details from us

4 Text Data Another term for one or more characters is a String JavaScript recognizes strings (as different from code) because they are enclosed in quotes You may use single or double quotes – must end with what you started with – e.g. "Steve" or 'Steve', not "Steve'

5 Handling Quotes in the Text How do you enclose a quote in the text – "Peter O'Toole" or – 'Peter O\'Toole' The backslash is an escape character – tells the compiler to treat the next character as text – or to do something special (e.g. advance to a newline)

Demo – Handling Quotes 6 Pause this slide and click the link below for a… video demonstration

Exercise Write a JavaScript program to display the following exact message (with the double- quotes) "Hey!", John said, "Don't do that!" 7

8 Special Characters \xNN – NN is a hexadecimal number which identifies a character in the Latin-1 character set – huh? – see Appx. D for translation of characters – e.g. "\x9 Paul Wilton" will print as: (c) Paul Wilton

9 Boolean A variable that can have a value of true or false is declared as a boolean. Example… boolean didItWork; diditWork = true;

10 Variables - Storing Data Data stored permanently or temporarily Permanent data is stored in a file or database Temporary data is stored in a variable – variable values are lost when the page is left – held in computers memory (RAM) Example: Storing bank account information vs. loan payoff information

11 Variable Naming Rules for Naming: – case sensitive – can't use reserved words (see Appx. B) – can't use certain special characters Example: &, % (see Appx. B) – can use numbers, but not as the first character

12 Question Which of these names is a valid variable name? – with – myVariable99 – my%Variable – my_Variable – my_Variable&Helper

13 Answer – with (no, reserved word) – myVariable99 (yes) – my%Variable (no, special character) – my_Variable (yes, underscore _ is allowed) – my_Variable&Helper (no, & not allowed)

14 I Declare! You should declare the existence of a variable by using the "var" keyword e.g. var myFirstVariable; Once declared, a variable can be store any type of data (weakly typed)

15 Assigning Values Use the equals ( = ) sign e.g. myFirstVariable = 101;

16 Garbage Collection The process of freeing up computer memory that was used to store variables is call "Garbage Collection" JavaScript does this automatically What would happen if it didn't?

17 Assigning Variables to Variables var myVariable; var myOtherVariable; myOtherVariable = 22; myVariable = myOtherVariable;

18 Basic String Operations Concatenation – appending one string to another – var concatString = "Hello " + "Paul"; – Spaces are significant

19 Mixing Numbers and Strings You can freely concatenate numbers and strings (the numbers are converted to strings automatically)

20 Exercise Code a JavaScript program that declares a variable that holds an age (in years) and a variable that holds a person's name and stores them in a variable in the following format… Hey Steve, you are 54 years old Display this message in an alert box

21 Data Type Conversion You may need to convert data from one type to another – String to integer [ parseInt( ) ] – String to floating points [ parseFloat( ) ]

22 Exercise Write a JavaScript program that prompts a user for their age Now substract 10 from the user's answer And display a message in the following format… Are you really only 44 years old?

23 Data that Won't Convert var myString = "I'm a name not a number!"; Returns NaN – Special Value – means Not a Number isNaN( ) function – e.g. myVar1 = isNaN("Hello"); TRUE – myVar2 = isNaN(34) FALSE

Null Values var nullString = null; var undefString; var blankString = ""; //has a value of empty string 24

End of Lesson 25