Introduction to JavaScript Data Types & Operators.

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

Introducing JavaScript
1 CS101 Introduction to Computing Lecture 21 Data Types & Operators (Web Development Lecture 7)
1 CS428 Web Engineering Lecture 12 Data Types & Operators (JavaScript - II)
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2: Introduction to C++.
JavaScript, Third Edition
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
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.
Introduction to scripting
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
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.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Introducing JavaScript. Goals By the end of this lecture you should … Be able to describe the differences among object methods, object properties and.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Variables.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Variables and Arithmetic Operators in JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Chapter 6 JavaScript: Introduction to Scripting
CHAPTER 5 SERVER SIDE SCRIPTING
Overview: Programming Concepts
JavaScript.
JavaScript an introduction.
Introduction to C++ Programming
During the last lecture we had a discussion on Data Types, Variables & Operators
During the last lecture we had a discussion on Objects, Properties, Methods
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10: Programming with javascript
Introducing JavaScript
Javascript Chapter 19 and 20 5/3/2019.
Lecture 9: JavaScript Syntax
Presentation transcript:

Introduction to JavaScript Data Types & Operators

Today’s Lecture … We will find out about JavaScript data types About variables We will also discuss various operators supported by JavaScript

JavaScript Data Types Unlike in C, C++ and Java, there are no explicit data types in JavaScript Nevertheless, it recognizes & distinguishes among the following types of values: –Numbers, e.g.,23, 4.3, -230, 4.4e-24 –Booleans, e.g.,true, false –Strings, e.g.,“hello”, “What’s the time?” –Undefined

We’ll comeback to these data types, but before that we have to define a few new terms First, variable:

Variables Variables give us the ability to manipulate data through reference instead of actual value Variables are names assigned to values Variables are containers that hold values (Example: Hotel guest name, Guest room no.) Generally, the value of a variable varies during code execution (that is why the term “variable”!)

Example x = 1; while (x < 6) { document.write (x); x = x + 1; } x is a variable

Try Doing the Same Without Using A Variable document.write (“1”); document.write (“2”); document.write (“3”); document.write (“4”); document.write (“5”); 5 lines of code replacing 5 lines of code! Why use variables?

Another Situation x = 1; while (x < 6000) { document.write (x); x = x + 1; }

Declaring Variables Many languages require that a variable be declared (defined) before it is first used Although JavaScript allows variable declaration, it does not require it - except in the case when we want to declare a variable being local (more on local variables later in the course!) However, it is good programming practice to declare variables before using them

Declaring Variables var height var name, address, phoneNumber

JavaScript Variables are Dynamically Typed Any variable in JavaScript can hold any type of value, and the type can change midway through the program This is unlike the case for C, C++ and Java, where a variable’s type is defined before usage The untyped feature makes JavaScript simpler to program in when developing short programs. However, this feature brings in a few problems as well. Can you describe any?

JavaScript Variables are Dynamically Typed var sum ; sum = 43 ; sum = “empty” ; After the execution of the 2nd statement, the data type becomes “number” After the execution of the 3rd statement, the data type changes to “string” After the execution of the 1st statement, the data type of the variable “sum” is “undefined”

Identifiers Identifiers are names used by JavaScript to refer to variables ( as well as objects, properties, methods, and functions!) An identifier must begin with an alphabetical character (a-z or A-Z) or the underscore “_” character Subsequent characters can be an alphabetical (a-z or A-B) or numeric character (0-9) or an underscore

Another Restriction on Identifiers JavaScript keywords cannot be used as identifiers For example, do not name a variable as “while”. When the browser sees this term in JavaScript code, it will get confused as it already knows this keyword as part of a loop statement. Same is the case for “var” or “if” or any of the other keywords.

JavaScript (Java) Reserved Words Names that can’t be used for variables, functions, methods, objects finallybyteimportthrowselse protectedgotowithdefaultnew abstractstaticclassinterfacevar floatcaseintransientextends publicifthisdonull Booleansuperconstlongvoid forcatch instanceof truefalse returnprivatethrowdoublepackage breakswitchcontinuenativewhile functioncharinttryfinal synchronizedimplements????

Avoid These Special Names As Well (1) Names that should not be used for variables, functions, methods, objects alertAreaassignBooleanCheckbox escapeFileUploadFormframesgetClass statusLinklocationMimeTypenavigate onunloadopenerPackagesparseFloatPassword setTimeoutStringsunTexttop AnchorArrayblurButtonSubmit evalfocusFrameFunctionHidden lengthLocationMathnameNavigator openOptionparentparseIntPlugin JavaPackagetaintTextareatoString

Avoid These Special Names As Well (2) Names that should not be used for variables, functions, methods, objects closeconfirmassignWindowJavaClass HistoryImageFormjavaonfocus navigatorNumberlocationonblurSelect promptRadioPackagesResetElement unescapevalueOfsunwindowJavaObject closedDateblurDocumentonload historyisNaNFrameJavaArraySelf netscapeObjectMathonerroruntaint prototyperefparentscrolltaint defaultStatusclearTimeoutdocument

Identifiers appear in JavaScript statements Let us now discuss a few other elements that appear in those statements

Elements of JavaScript Statements b = 2 ; sum = sum + 49 ; name = “Bhola” + “ Continental” ; x = Math.floor ( x ) ; Identifiers Operators Literals Punctuation

JavaScript Literals A data value that appears directly in a statement Literals can be of several types. Some of them are: 1.Number 2.String 3.Boolean

Numeric Literals e-27 JavaScript stores all numbers, even integers, as floating-point numbers

String Literals “” ‘’ ‘Bhola’ “Where is the Bhola Continental Hotel?” String literals are always enclosed in a matching pair of single or double quotes

Boolean Literals true false if ( tankFull == false) addMoreWater = true

JavaScript Operators Operators operate on operands to achieve the desired results JavaScript has numerous operators, classified in many categories. We will look at only a few of them belonging to the following categories: –Assignment operators-- Arithmetic operators –Comparison operators-- String operators –Logical operators We’ll look at a few more during future lectures, but understand that there are many more.

Assignment Operator “=” Changes the value of what is on the LHS, w.r.t. what is on the RHS total_number_of_students = 984 ; title = “Understanding Computers” ; swapFlag = false ; x = y + 33 ;

Arithmetic Operators Multiply2 * 4 → 8 Divide 2 / 4 → 0.5 Modulus5 % 2 → 1 Add2 + 4 → 6 Subtract2 - 4 → -2 Negate-(5) → -5

Comparison Operators The “equal to (==)” Comparison Operator if ( today == “Sunday” ) document.write(“The shop is closed”); The string “The shop is closed” will be written to the document only if the variable today has a value equal to “Sunday” Not the same as the assignment “=” operator

Comparison Operators a == b True if a and b are the same a != b True if a and b are not the same a > b True if a is greater than b a >= b True if a is greater than or equal to b a < b True if a is less than b a <= b True if a is less than or equal to b

Example if ( x != 0 ) result = y / x; else result = “not defined”;

From comparison operators, we move to Logical Operators

Logical Operators Operate on Boolean expressions or variables The “AND (&&)” Logical Operator if ( (pitch == “hard”) && (bowler == “fast”) ) myStatus = “Pulled muscle”; The value of the variable myStatus will be set to “Pulled muscle” if both of the conditions are true

Logical Operators a && bAND True if both are true a || bOR True of either or both are true !aNOT True if a is false

Example if ( x || y ) document.write (“Either or both are true”); else document.write (“Both are false”);

So far we have looked at the assignment operator, arithmetic operators, comparison operators and logical operators The final category that we are going to look at is string operators In that category, we look at only one, the concatenation operator

The “+” String Operator The “+” operator can be used to concatenate two strings title = “bhola” + “continental” The value of the variable title becomes “bholacontinental”

Semicolon ; Terminate all JavaScript statements with a semicolon. It is not always necessary, but highly recommended. a = 23 ; quotient = floor( a / 2) ; remainder = a % 2 ;

Elements of JavaScript Statements b = 2; sum = sum + 49; name = “Bhola” + “ Continental”; x = Math.floor ( x ); Identifiers Operators Literals Punctuation

Two more elements that are found in JavaScript statements are white spaces and line breaks

White Spaces & Line Breaks White spaces: The space & the tab characters JavaScript ignores any extra white spaces or line breaks that you put in the code This gives you the freedom of using them for making your code appear neat and readable

while ( x > 0) { remaind = x % 2; y = remaind + y; } while ( x > 0) {remaind = x % 2; y = remaind + y;} while ( x > 0) { remaind = x % 2; y = remaind + y; }

Now let’s talk about a very special type of JavaScript statement that does not really do anything, but is found in most pieces of code!

Comments Comments are included on a Web page to explain how and why you wrote the page the way you did Comments can help someone other than the author to follow the logic of the page in the author’s absence The commented text is neither displayed in the browser nor does it have any effect on the logical performance of the Web page, and is visible only when the actual code is viewed

JavaScript Comments Single-line comments (two options) // Author: Bhola <!-- Creation Date: 24 March 2003 Multi-line comments /* Author: Bhola Creation Date: 24 March 2003 */

HTML Comments <!-- Author: Any Creation Date: 24 March >

x = 75 ;// x is the decimal number y = “” ;// y is the binary equivalent while ( x > 0) { remainder = x % 2 ; quotient = Math.floor( x / 2 ) ; y = remainder + y ; x = quotient ; } document.write(“y = ” + y) ; Decimal to Binary Conversion in JavaScript