Chapter 3 Data Types and Variables

Similar documents
JavaScript: The Basics

Client-Side Web Technologies. JavaScript Part I

3 The Building Blocks: Data Types, Literals, and Variables

CGS 3066: Spring 2015 JavaScript Reference

CSC Web Programming. Introduction to JavaScript

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Chapter 2 Working with Data Types and Operators

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

HTML5 and CSS3 More JavaScript Page 1

Java Basic Datatypees

Such JavaScript Very Wow

Sprite an animation manipulation language Language Reference Manual

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

JavaScript: Sort of a Big Deal,

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

CS1520 Recitation Week 2

Number Systems, Scalar Types, and Input and Output

1.1 Introduction to C Language. Department of CSE

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

2 nd Week Lecture Notes

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

CS112 Lecture: Primitive Types, Operators, Strings

Programming in C++ 4. The lexical basis of C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

String Computation Program

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JAVA Programming Fundamentals

Work relative to other classes

Slang Reference Manual

egrapher Language Reference Manual

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

JavaScript Basics. Mendel Rosenblum. CS142 Lecture Notes - JavaScript Basics

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

Data Types Literals, Variables & Constants

JavaScript: Coercion, Functions, Arrays

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JavaScript Lecture 1

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Variables and Literals

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

CS149: Elements of Computer Science. Fundamental C++ objects

Basic Types, Variables, Literals, Constants

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

Chapter 2: Using Data

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

JavaScript I Language Basics

COMP6700/2140 Data and Types

CMPT 125: Lecture 3 Data and Expressions

Introduction to Regular Expressions Version 1.3. Tom Sgouros

These are notes for the third lecture; if statements and loops.

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

CHAPTER 5: JavaScript Basics 99

PHP. Interactive Web Systems

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Introduction to C Language

JME Language Reference Manual

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

What is PHP? [1] Figure 1 [1]

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

CS11 Java. Fall Lecture 1

Program Fundamentals

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Topic 9: Type Checking

Topic 9: Type Checking

CS 115 Lecture 4. More Python; testing software. Neil Moore

VLC : Language Reference Manual

Title:[ Variables Comparison Operators If Else Statements ]

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 4 Basics of JavaScript

Language Reference Manual

CS312: Programming Languages. Lecture 21: JavaScript

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

Javascript Arrays, Object & Functions

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

C# and Java. C# and Java are both modern object-oriented languages

Declaration and Memory

Intro. Scheme Basics. scm> 5 5. scm>

Variables, Constants, and Data Types

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Transcription:

Chapter 3 Data Types and Variables Adapted from JavaScript: The Complete Reference 2 nd Edition by Thomas Powell & Fritz Schneider 2004 Thomas Powell, Fritz Schneider, McGraw-Hill

Jargon Review Variable container to hold data in a script Identifier the name of a variable Literal data that appears directly in source code (e.g. 5, thomas, true) Type describes the format/nature of the data Primitive JavaScript types: string, number, Boolean Complex JavaScript types: function, array, object

Jargon Review Contd. There are strong, weak, and untyped languages g JavaScript is a weakly typed language for better or worse When you declare a variable you are setting up space to hold data var x; When you assign a variable you put some data into it (associate it with something) x = "foo"; ;

Variables Variables are defined with the var keyword var myname = "Fred"; var x; var x,y,z; var a = 1, x="hello", longername; Variable names or more appropriate p identifiers should start with a letter and may be followed by any number of letters, digits, underscores, or dashes Variable identifiers must not contain special characters or white space

Numbers JavaScript doesn t make great distinction to the type of numbers unlike some languages Numbers are represented in 64bits though with integers you should understand that it may be a 32bit representation Ranges for JavaScript are typically: Ranges for JavaScript are typically: Floating point range: -2.2250 x 10-308 +1.7976x10 308 Integer Range 2147483648-2147483647

Numbers Contd. Format of numbers include: DecimalDigits.(DecimalDigits)(Exponent).DecimalDigits(Exponent) DecimalDigits(Exponent) All the following are valid numbers in JavaScript 10, -2.71,.4444e7, -1.7E12, 3.5E-5, 128E+100 The following are not numbers 2,717, 22e100.5, 128e5e10, e10 Do not use leading zeros in JavaScript numbers since it has special meaning discussed next

Hex and Octal Literals You can specify base-16 (Hex) and base-8 (octal) numbers in JavaScript Hex has digits 0-9 and A F(1015) (10-15) Format 0x(Hex digits) Examples: 0x0, 0xF8F00, 0xFFFFFF Value is not obvious on Web except color representations ti Octal is not part of ECMA-262 spec but widely supported by JavaScript implementations i Format 0(octal digits 0 7) Examples 00, 0777, 0245

Special Number Values These special values are both useful and occasionally troublesome Property Number.MAX_VALUE Number.MIN_VALUE Number.POSITIVE_INFINITY Number.NEGATIVE_INFINITY Number.NaN Value Largest magnitude representable Smallest magnitude representable Infinity Negative Infinity Not a number Note: You may end up with these values when you do something extreme (error case)

Strings In JavaScript a string is surrounded by either single or double quotes ( t (t, Thomas ) The choice of quotes is interchangeable in the base case but there is reason to be aware of which to use and when Strings like numbers are associated with an underlying String object. var mystring = new String("Thomas"); alert(mystring.charat(2)); alert(mystring.length); JavaScript string are not thought of as arrays though (ex. JavaScript string are not thought of as arrays though (ex. alert(mystring[1])

Special Characters and Strings You can add special characters to strings using the following escape codes Escape Code Value \b Backspace \t Tab \n Linefeed \v Vertical Tab \f Form feed \r Carriage return \ Double quote \ Single quote \\ Backslash Note: Because of final display environment from JavaScript is often an (X)HTML based web page, p y p ( ) p g, escape codes may not always produce intended results because of whitespace rules.

Booleans Booleans are the logical data type and take on a value of true or false Often used in conditionals and loops var doincrement = true; while (doincrement) { } Some languages assume 0 = false and 1 = true being used interchangeably. While conversion may make this seem true, it isn t. Non-zero = true and 0 = false, but try not to assume numeric values when using Booleans.

Undefined and Null The undefined type is used for variables or object properties that either do not exist or have not been assigned a value The null value indicates an empty or non-existent value Placeholder for nothing typeof null returns object Null and undefined aren t really the same thing so be careful. However, note that they will compare as equal!

Composite Types An object is a composite type that can contain primitive and composite types, including functions and other objects The members of an object are called properties The member functions are called methods Properties are accessed with the. operator navigator.appnameappname Methods are accessed the same way except you add ( )s since a function is being called window.close( ); document.write("hi there");

Objects We ll see in JavaScript there are four builtin object types 1. Type related String, Number, Boolean, Array, Object 2. Built-in Date, Math, RegExp 3. Browser Window, Navigator 4. Document Document, Form, Image, etc.

Objects A fifth type would be user created, but that is somewhat rare given the coding style used by many JS programmers as well as the applicability of OOP principles to extremely small scripts. In general objects are created with the new operator as well as the appropriate p object constructor var mylocation = new Object(); mylocation.city = "San Diego"; mylocation.state = "California";

Arrays An array is an ordered list that can contain primitive and complex data types. Array can be declared explicitly or created using the Array object constructor (Arrays are objects too!) var myarray y = [5,67,45243, "hello"]; var myarray2 = new Array(); // var myarray2 = [ ]; Arrays are zero based indexed Arrays elements are accessed with the [ ] operator alert myarray[0]; alert(myarray[1]); alert(myarray[3]); y alert(myarray[4]); myarray[4] = "hi there";

Arrays As we can see with assigning to new locations, arrays are extended automatically myarray[500] = "wow!"; Sparse arrays may result! Be careful about memory issues even though JavaScript does use garbage collection. Arrays are objects and they have many properties and methods alert(myarray.length); myarray.reverse(); See Chapter 7 for details on the numerous properties and methods for Array.

Functions Functions are objects and thus are a data type Function are declared d like so: function functioname(parameter list) { function statement(s) optional return statement(s) } Functions are called by invoking function name with a list of parameters functioname(parameter1, parameter n); We wait until Chapter 5 to study functions in depth

Variable scope The scope of a variable is the part of a script where it is visible (known) Two scopes global [ document wide ] local l [ limited it to defining i function] The var keyword when used in a function creates a local variable. We will revisit this topic in depth later on

Type Conversion Converting one type of data to another is both useful and a source of numerous errors in JavaScript var x = "10" 2; // result is 8 var x = "2" 2 - "2"; 2 ; // result is 0 var x = "2" + "2"; // result ="22" var BooleanData = true; var numericdata = 3.14 alert( numericdata >= BooleanData)

Type Conversion You may run into some serious problems with equality (==) thus you should use the type equality operator (===) and consider performing type checks using the typeof operator The next few slides show the basic type conversions you should be aware of.

Conversion to Boolean Table Type Undefined Null Number Converted to Boolean False False False if 0 or Nan otherwise true String False if string is empty (length == 0) otherwise true Other objects true

Conversion to Number Table Type Undefined Converted to Number NaN Null 0 Boolean String Other objects 1 if true, 0 if false The number in the string or NaN if no number within string NaN

Conversion to String Table Type Undefined Null Boolean String Other objects Converted to String undefined null true if true, false if false NaN, 0, or a string representing the number Value of the object s tostring() method otherwise undefined

typeof Operator Values You should be aware of the values returned by typeof when checking conversions Type Undefined Null Boolean Number String Object Function Result of typeof undefined object boolean number string object function

Summary JavaScript provides five primitive data types: number, string, Boolean, undefined, and null Composite (or if you like complex) data types include objects, arrays, and functions, but underneath everything tends to be an object Two primary scopes exist (global and local) The language is weakly typed and conversion should be well understood