CHAPTER 5: JavaScript Basics 99

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

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

Client-Side Web Technologies. JavaScript Part I

CSC Web Programming. Introduction to JavaScript

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

JAVA Programming Fundamentals

Language Fundamentals Summary

HTML5 and CSS3 More JavaScript Page 1

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

CGS 3066: Spring 2015 JavaScript Reference

Lecture 3. More About C

Exercise 1: Basic HTML and JavaScript

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Chapter 8 JavaScript/JScript: Introduction to Scripting 201

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

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

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

UNIT- 3 Introduction to C++

334 JavaScript/JScript: Functions Chapter 11. boss. worker1 worker2 worker3. Hierarchical boss function/worker function relationship.

CSC 1214: Object-Oriented Programming

Outcomes Week 2 Overview Describe the inputs, activities, and outputs of each step in the software development life cycle. Describe arithmetic, relati

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

C - Basic Introduction

Expression and Operator

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

JavaScript CS 4640 Programming Languages for Web Applications

DEPARTMENT OF MATHS, MJ COLLEGE

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

Java Programming Fundamentals. Visit for more.

JScript Reference. Contents

Types, Operators and Expressions

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

A Java program contains at least one class definition.

write vs. writeln Prompting as Page Loads Today s Goals CSCI 2910 Client/Server-Side Programming Intermediate File vs. HTML Output

Chapter 2 Elementary Programming

Types, Operators and Expressions

A flow chart is a graphical or symbolic representation of a process.

Java Basic Datatypees

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Quick Reference Guide

Quick Reference Guide

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

Basic Types, Variables, Literals, Constants

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C Basics. Chapter 2: C Basics. C Programming Language. v3.3 February 6, 2017

Information Science 1

Variables and Typing

Program Fundamentals

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

COMP2121: Microprocessors and Interfacing. Number Systems

JAVASCRIPT - OPERATORS

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Operators. Java operators are classified into three categories:

Java Notes. 10th ICSE. Saravanan Ganesh

Operations. Making Things Happen

Declaration and Memory

Chapter 3 Data Types and Variables

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

SECTION II: LANGUAGE BASICS

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

File Handling in C. EECS 2031 Fall October 27, 2014

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

PYTHON- AN INNOVATION

Programming. Elementary Concepts

Index COPYRIGHTED MATERIAL

Work relative to other classes

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Introduction to C# Applications

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

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

JavaScript I Language Basics

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

JavaScript CS 4640 Programming Languages for Web Applications

Number Systems, Scalar Types, and Input and Output

Basics of Java Programming

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Arithmetic and Bitwise Operations on Binary Data

Object-Oriented Programming

Arithmetic and Bitwise Operations on Binary Data

Fundamental of C programming. - Ompal Singh

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CSC 1107: Structured Programming

3. Java - Language Constructs I

Transcription:

CHAPTER 5: JavaScript Basics 99 5.2 JavaScript Keywords, Variables, and Operators 5.2.1 JavaScript Keywords break case continue default delete do else export false for function if import in new null return switch this true typeof var void while with Reserved Keywords for future extensions catch class const debugger enum extends finally super throw try 5.2.2 Variables and Values Value Types Type Description Example Number Any numeric value 10.145 String Character inside quotation marks "Web Prog" Single quotes or double quotes 'testing' Boolean True or False false NULL Empty value Objects Compound data types that contains methods and properties

CHAPTER 5: JavaScript Basics 100 Examples of Number 10 3.1416 0.003 6.20E3 0377 // Octal number; or 255 in decimal 0xFF // Hex number; or 255 in decimal Examples of String 'ECET Dept' "3.14 16" 'Name = "thisform"' Variable Declaration var n, m; // uninitialized var x = 100, message = 'Hello you'; Escape Sequence \b Backspace \f Form feed \n New line \r Carriage return \t Tab \' Apostrophe or single quote \" Double quote \\ Back slash \ddd Three octal digits (d: 0,1, through 7) \xdd Two hexadecimal digits (d;0, 1,2,..,9, A,B,C,D,E,F) \udddd Unicode encoding by four hex degits

CHAPTER 5: JavaScript Basics 101 Arrays var n = new Array(); // Empty array var b = new Array(20); // 20 elements var k = new Array(1, 2, 3); // Initialized with three elements JavaScript 1.2 var k = [1, 2, 3]; var anyobjet = [1, true, [1,2], {x:1, y:2}, "Hello"]; Null and Undefined null - no value 5.2.3 Operators Conditional Operator (? : ) Math.abs(x) or x < 0? -x: x typeof operator Evaluate the type of object: "number", "string", or "boolean" new operator Creating new objects delete operator Delete existing objects

CHAPTER 5: JavaScript Basics 102 Arithmetic Operators + x + y Add and assign + x + y String concatenation - x - y Subtraction * x * y Multiplication / x / y Division % x%y Modulus of x and y (remainder operation) ++ x++, ++x Increment -- x--, --x Decrement - -x Negate (change sign) Bitwise Operators & x & y bitwise AND; both x and y are unsigned integer x y bitwise OR; both x and y are unsigned integer ^ x ^ y bitwise XOR; both x and y are unsigned integer ~ ~x bitwise NOT << x << 2 Shift left 2 bit (times 4) >> x >> 2 Shift right 2 bit (div by 4) >> x >> 2 Shift right zero fill (div 4)

CHAPTER 5: JavaScript Basics 103 Assignment Operators = x = y Assignment, copy value += x += y Add and assign; x = x +y -= x -= y Sub and assign; x = x -y *= x *= y Multiply and assign; x = x * y /= x /= y Divide and assign; x = x / y %= x %= y Modulus and assign; x = x % y <<= a <<= b a = a << b >>= a >>= b a = a >> b >>>= a >>>= b a >>>= b &= a & b a = a & b = a = b a = a b ^= a ^= b a = a ^ b Relational Operators == x == y Equal to; return true if x and y are equal!= x!= y Not equal; return true if x and y are not equal > x > y Greater than; return true if x is greater than y >= x >= y Greater than or equal to ; return true if x is greater than or equal to y < x < y Less than; return true if x is less than y <= x <= y Less than or equal to; return true if x is less than or equal to y && x && y Logical AND; Return true if both x and y are true x y Logical OR; return true if either x or y is true!!x Logical NOT; return true if x is false

CHAPTER 5: JavaScript Basics 104 Global Constant Infinity NaN - not a number Global Functions escape(s) Encode a string for transmission unescape(s) Decode an escaped string eval(code) Execute JavaScript code from a string getclass(javaobj) Return JavaClass of a JavaObject isfinite(n) Determine if a number is finite isnan(x) Determine if a number is defined parsefloat(s) Convert a string to a floating poit number parseint(s, radix) Convert a string to an integer

CHAPTER 5: JavaScript Basics 105 Example 5-5: Declare and use JavaScript variables. <HTML> <!-- variable.htm --> <HEAD> <TITLE>Example of Using Variables</TITLE> <SCRIPT language=javascript> var num1 = 10; // a number variable var num2 = 212.00;// a float number var lastname ="Lin"; // a string variable var phoneno ='481-6339'; // a string variable var outofjob = false; // a boolean variable document.write("num1 document.write("num2 document.write("lastname document.write("phoneno document.write("outofjobn </SCRIPT> = "+ num1 + "<BR>"); = "+ num2 + "<BR>"); = "+ lastname + "<BR>"); = "+ phoneno + "<BR>"); = "+ outofjob + "<BR>"); </HEAD> <BODY> </BODY>

CHAPTER 5: JavaScript Basics 106 Example 5-6: Enter two numbers and make a comparison. <html> <!-- twonums.htm --> <Head> <Title>Comparing Two Numbers</title> <Script Language="JavaScript"> <!-- var num1str, num2str; // Desine varaibels var num1int; //first integer value var num2int; //second integer value //read first number from user as string num1str = window.prompt ("Enter first integer","0"); num1int = parseint(num1str); //Convert string to integer //read second number from user as string num2str = window.prompt ("Enter second integer","0"); num2int = parseint(num2str); //Convert string to integer document.writeln("<h1>"+ "First Numebr = " + num1str +"</H1>"); document.writeln("<h1>"+ "Second Numebr = " + num2str +"</H1>"); // Comparing two numbers if (num1int > num2int) document.writeln("<h1>"+ num1str + " is larger</h1>"); if (num1int < num2int) document.writeln("<h1>"+ num2str+ " is larger</h1>"); if (num1int == num2int) document.writeln("<h1>two numbers are equal</h1>"); //--> </Script> </head> <body> <p>click refresh (or reload) to run the script again</p> </body> </html> Output:

CHAPTER 5: JavaScript Basics 107

CHAPTER 5: JavaScript Basics 108 Example 5-7: Use while loop to generate a series of numbers and determine the even or odd number properties using decision making statement - if. <HTML> <!-- while_if.htm --> <HEAD> <TITLE>Control: while, if, and continue</title> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> <!-- var numvar = 0; while (numvar < 7) { numvar = numvar + 1; if ((numvar % 2)== 0 ) { document.write("number is even.<br>"); continue; } document.write("the number is " + numvar + ".<BR>"); } --> </SCRIPT> </BODY> </HTML> Output:

CHAPTER 5: JavaScript Basics 109 Example 5-8: Create a number table with four columns and 5 rows. <html> <Head> <!-- numtable.htm --> <Title>Number Table</title> <Script Language="JavaScript"> // Define variables var num = 0; //num variable var num10 = 0; //10* num var num100 = 0; //100* num var num1000 = 0; //1000*num //create a table and print headings document.writeln("<h2 ALIGN = 'center'>number Table</H2>"); document.writeln("<table border='1' ALIGN = 'center' width='60%'>"); document.writeln("<tr><td><u>n</u></td><td><u>10*n</u></td><td><u>100*n </U></TD><TD><U>1000*N</U></TD></TR>"); num =1; //initialize number to its first value in the while (num <=5) { num10 = 10 * num; num100 = 100 * num; num1000 = 1000 * num; document.writeln("<tr><td>"+ num + "</TD><TD>" + num10 + " </TD><TD>" + num100 + " </TD><TD>" + num1000 + "</TD></TR>"); //print entries num++; } document.writeln("</table>"); </Script> </head> <body></body> </html>

CHAPTER 5: JavaScript Basics 110 Example 5-9: Customer account and credit evaluation example. <html> <!-- account.htm --> <head> <title>customer Account Info</title> <Script Language="JavaScript"> // Define string variables var acctnumber; //customer's account number var balance; var charge; // total of all items charged by this customer // for this month var credit; //total of all credits applied to this customer var creditlimit; //Define variables for holding numerical value var valbalance; // balance as a Number var valcharge; var valcredit; var valcreditlimit; // Read inputs acctnumber=window.prompt("enter customers account number","0"); balance=window.prompt("enter begining balance","0"); valbalance=parseint(balance); charge=window.prompt("enter total charge amount for this customer","0"); valcharge=parseint(charge); credit=window.prompt("enter total credit amount for this customer","0"); valcredit=parseint(credit); creditlimit=window.prompt("enter total credit limit for this customer","0"); valcreditlimit=parseint(creditlimit); valbalance += (valcharge - valcredit); document.writeln("<h1>new balance for customer # " + acctnumber+ " = $" + valbalance + "</H1>"); if (valbalance > valcreditlimit) document.writeln("<h1>credit Limit Exceeded</H1>"); </script> </head> <body> <p>click Refresh (or Reload) to run the script again</p> </body> </html>

CHAPTER 5: JavaScript Basics 111

CHAPTER 5: JavaScript Basics 112