JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Data types and variables
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Tutorial 14 Working with Forms and Regular Expressions.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
String Escape Sequences
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Objectives You should be able to describe: Data Types
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Data types, Literals (constants) and Variables Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within.
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.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CS346 Javascript -3 Module 3 JavaScript Variables.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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?
Chapter 2 Variables.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Variables.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 4 – Simple Variable Types Modern JavaScript Design And Develop Copyright © 2012 by Larry.
JSON (Copied from and from Prof Da Silva) Week 12 Web site:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 2 Variables.
Chapter 6 JavaScript: Introduction to Scripting
Java Variables and Types
Documentation Need to have documentation in all programs
Wel come.
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
Section 3.2c Strings and Method Signatures
Java Programming: From Problem Analysis to Program Design, 4e
JavaScript and Ajax (Expression and Operators)
Chapter 2: Basic Elements of Java
T. Jumana Abu Shmais – AOU - Riyadh
Variables Kevin Harville.
Chapter 2: Introduction to C++.
The Data Element.
The Data Element.
Chapter 2 Variables.
Presentation transcript:

JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:

JavaScript Data Types Three primitive data types 1.number (internal represented as float number) 2.string 3.boolean One composite data type object (object, array, function) Four special values 1.null (no value) 2.undefined (variable declared, but no value assigned to it; object with property that does not exist) 3.NaN (Not a Number) 4.infinity (+ or – infinity)

Primitive Data Type - number Number SystemBaseExample Decimal10127 Hexadecimal160x7F or 0X7F Octal80177 MethodExample Normal Scientific notation12E E1.552E0

Primitive Data Type - string A string is a sequence of Unicode letters, digits, punctuation characters, and so on Examples: "" 'Testing' "3.14" "Last name is O’Riley" "This string has \n two lines"

Escape Sequence Escape sequence in string literals: the backslash character (\) has a special purpose in JavaScript strings. Combined with the character that follows it, it represents a character that is not otherwise representable within the string. Escape sequenceDescription \0The null character \bBackspace \tHorizontal tab \nNewline \vVertical tab \fForm feed \rCarriage return \"Double quote \'Apostrophe or single quote \\backslash

Primitive Data Type – string (continue…) Creating a string object: var test = "this is a text"; var test = new String("this is a text"); Concatenate strings: var msg = "Hello, " + "World"; // produce "Hello, World“ var greeting = "Welcome " + msg;

String Methods var test = "this is a text"; test.length // length of string = 14 test.toUpperCase() // output string to uppercase, original data unchanged test.toLowerCase() // output string to lowercase test.substring(2,4) // extract chars from pos 2 to pos 3, starting pos zero test.charAt(test.length – 1) // single char test.indexOf("is") // find the first occurrence of is, 2 test.indexOf("is", 3) // find "is" starting from position 3

Primitive Data Type - boolean The boolean data type has only two values: true and false  true = 1, +/- number, non-empty string  false = 0, NaN, "", null, undefined Boolean value used as number context: var a = true; // what data type is a? var b = a + 1; // what is b? Boolean value used as string context: document.write(a); // display "true" or "false"

Primitive Data Type - boolean Number value used as boolean context: var num = 1; // what is the result? If num=0 or NaN if (num) { alert("true"); } else { alert("false"); } String value used as boolean context: var num = "abc"; // what is the result? If num = "" or null or undefined if (num) { alert("true"); } else { alert("false"); }

Data Conversion Number: var num = 4.5;  Auto conversion from number to string alert(num); document.write(num);  Auto conversion from number to boolean if (num) { alert("true"); } else { alert("false"); }  Manual conversion from number to string alert(num.toFixed(2)); // what is the result? //4.50 alert(num.toExponential(2)); // 4.50E0 alert(num.toPrecision(2)); //4.5

Data Conersion (continue…) String: var str1 = "21"; var str2 = "2";  Auto conversion from string to number var result = str1 * str2; // other operators / - are ok too if (str2 == 2) { alert("true"); }  Auto conversion from string to boolean if (str2) { alert("true"); }  Manual conversion from string to number var num = parseInt(str1); var num2 = parseFloat(str2);

Data Conversion (continue…) Boolean: var bool = true;  Auto conversion from boolean to number var result = bool + 2; if (bool == 1) { alert("true"); }  Auto conversion from boolean to string alert(bool); document.write(bool);  No Manual conversion

Composite Data Type - object Object is a composite data type which consists of properties and methods Dot notation: object_name.property_name object_name.method_name() Object creation:  Special constructor (new operator): var obj = new Object(); obj.x = 2.3; obj.y = 3.2;  Object literal: var obj = { x:2.3, y:3.2};

Composite Data Type - array Array is a collection of data values (elements), each data value (element) in an array has an index (non-negative integer, starting from zero) Array creation  With new operator: var a = new Array(); a[0] = 12; a[1] = "ABC"; a[2] = true;  Declare and initialize at the same time: var a = new Array(12, "ABC", true);  With array literal: var a = [12, "ABC", true];

Associative array In JavaScript, object and array are interchangeable Object creation: var obj = { x:2.3, y:3.2}; Referencing object using dot notation: obj.x = 123; Referencing object using associative array format: obj["x"] = 123;

Composite Data Type - function Function is a piece of executable code function function_name(argument, parameter) { // JavaScript code; } // define JavaScript function function square(x) { return x * x; } // call user defined function var y = square(2);

NaN var num = parseInt("abc"); // or use parseFloat() if (isNaN(num)) { alert("Not a number"); } else { alert("It is a valid number"); }

variable Variable is a symbolic name that represents a value that can and likely will change or vary during a program’s execution Normally, declare and initialize at the same time var n = 37; var a = "abc"; var b = true; Variable name rules: 1.Only letters, numbers, and underscore are valid characters 2.First character must be letter or underscore 3.Cannot contain spaces 4.No reserved words 5.It is case sensitive

variable (continue…) Invalid variableValid variable first-namefirst_name 1namefirst_name last namelast_name casemyCase ifmyIf

Expression Expression will be evaluated to return a single value (single data type) var num = ; First, will be evaluated to become 29 with data type of number Then, assign numeric 29 to variable num

Sum of two numbers JavaScript treats data in HTML text box as string data type even though it may contain number [See sum of two numbers sample]sum of two numbers sample

Sum of two numbers solution To fix the problem, converts the text/string data manually using parseInt() or parseFloat() [See sum of two numbers solution]sum of two numbers solution

Typeof operator Typeof operator determines the current data type of any variable var a = 123; var b = true; var c = "ABC"; var result = typeof(a);