CSC Web Programming. Introduction to JavaScript

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

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

Client-Side Web Technologies. JavaScript Part I

CGS 3066: Spring 2015 JavaScript Reference

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

COMS 469: Interactive Media II

Visual C# Instructor s Manual Table of Contents

HTML5 and CSS3 More JavaScript Page 1

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

Such JavaScript Very Wow

JavaScript: The Basics

C-LANGUAGE CURRICULAM

PHP. Interactive Web Systems

CHIL CSS HTML Integrated Language

Java Primer 1: Types, Classes and Operators

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

Chapter 2: Using Data

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

c122mar413.notebook March 06, 2013

JavaScript I Language Basics

CS313D: ADVANCED PROGRAMMING LANGUAGE

ARG! Language Reference Manual

Chapter 2: Using Data

Introduction to Programming Using Java (98-388)

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

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

BASIC ELEMENTS OF A COMPUTER PROGRAM

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Elementary Programming

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

egrapher Language Reference Manual

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

introjs.notebook March 02, 2014

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

Chapter 2 Working with Data Types and Operators

Typescript on LLVM Language Reference Manual

CSI33 Data Structures

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Variables and Typing

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

JavaScript Basics. The Big Picture

Working with JavaScript

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

corgi Language Reference Manual COMS W4115

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Introduction to C# Applications

Chapter 17. Fundamental Concepts Expressed in JavaScript

Basics of Java Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Server side basics CS380

Sprite an animation manipulation language Language Reference Manual

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Program Fundamentals

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

JME Language Reference Manual

FRAC: Language Reference Manual

CSC 1107: Structured Programming

JAVA Programming Fundamentals

Exercise 1: Basic HTML and JavaScript

CSCE 120: Learning To Code

Programming language components

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

JavaScript: Introduction, Types

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

Language Reference Manual

JavaScript: Sort of a Big Deal,

Full file at

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

Decaf Language Reference Manual

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

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

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

UNIT- 3 Introduction to C++

3. Java - Language Constructs I

Web Site Design and Development JavaScript

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

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

Pace University. Fundamental Concepts of CS121 1

JavaScript: Introductionto Scripting

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

PHP by Pearson Education, Inc. All Rights Reserved.

The Warhol Language Reference Manual

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

Transcription:

CSC 242 - Web Programming Introduction to JavaScript

JavaScript JavaScript is a client-side scripting language the code is executed by the web browser JavaScript is an embedded language it relies on its host environment for IO JavaScript IO options: console.log: writes output to the browser console alert: writes output to pop-up window document.write: writes output to the HTML document

The script Element JavaScript source code is placed in an HTML document within the script element An external JavaScript source file can be imported with the src attribute: < script type =" text / javascript " src =" url / file.js"> </ script > The <noscript> tag defines alternate content when JavaScript is disabled or not available

Basic Syntax A statement does not need to be terminated by a semicolon when it is the only statement on a line Line comments are denoted by // Block comments are denoted by /*... */

Simple Example <! DOCTYPE html > <html > <head > <title > Hello World </ title > </head > <body > <script > document. write (" Hello World "); </ script > <noscript > Your browser does not support or has disabled JavaScript </ noscript > <body > </html >

JavaScript Variable Naming Rules A variable may include only the characters a-z,a-z,0-9, the $ symbol, and the underscore ( ) No other characters are allowed in a variable name The first character in a variable name must be a letter, $, or Variable names are case sensitive

JavaScript Types JavaScript data types: Object Function Number: (Integer and Float) String Boolean Null Undefined JavaScript is dynamically typed types of variables do not need to be declared JavaScript is weakly typed some type conversions are automatic

The String Type The string type represents a sequence of characters The string type must be enclosed by single or double quotes The escape character is the backslash (\) The plus (+) operator performs string concatenation

Multi-line strings A string can be defined over multiple lines by escaping the newline character name = " first_name \ last name ";

Arithmetic Operators Operator Description Example + Addition a + 3 - Subtraction a - 3 * Multiplication a * 3 / Division a / 3 % Modulus a % 3 ++ Increment ++a -- Decrement --a

Assignment Operators Operator Example Equivalent to = a = 3 a = 3 += a += 3 a = a + 3 += a += 3 a = a + text -= a -= 3 a = a - 3 *= a *= 3 a = a * 3 /= a /= 3 a = a / 3 %= a %= 3 a = a % 3

JavaScript Implicit Type Coercion The type of a variable is implicitly converted based on the context in which the variable is used <script > x = "10"; // string y = 3.14; // number z = x * y; // number </ script > The typeof function returns a string representation of a variable s type

Explicit Type Casting Functions parseint() cast to Int, Integer Boolean() cast to boolean parsefloat() cast to Float, Double, Real String() cast to string split() cast to array

Equality & Comparison Operators Operator Description Example == equal to a == 3 === identical to a === 3!= not equal to a!= 3!== not identical to a!== 3 > greater than a > 3 < less than a < 3 >= greater than or equal to a >= 3 <= less than or equal to a <= 3

Logical Operators Operator Description Example && and a == 3 && b == 0 or a == 3 b == 0! not!(a == b)

Selection if, else, and else if if (a > 100) { document. write (" >")} else if (a < 100) { document. write (" <")} else { document. write ("=")} switch switch ( page ) { case (" Home "): document. write (" Home "); break ; case (" About "): document. write (" About "); break ; default : break ;

Iteration while loops do while loops for loops Example: for ( var count = 1; count <= 10; ++ count ) { document. write (" Count :" + count + "<br >"); } break and continue

Defining a JavaScript Function function function_name ([ parameter [,...]]) { // Statements } [ return ] A definition starts with the word function Next is the name of the function, which must start with a letter or underscore, followed by any number of letters, numbers, or underscores Function names are case sensitive The parentheses are required Zero or more parameters, separated by commas A value can be returned from a function with the return keyword

Variable Scope Local variables are accessible in context in which they are defined Global variables are accessible from all parts of the code Function parameters have local scope The var keyword defines a local variable with a scope of the current function Example: function test () { a = 123 // global var b = 456 // local if (a == 123) { var c = 789 // local } }

JavaScript Objects A JavaScript object groups data with functions that manipulate it The data members of an object are referred to as properties The functions of an object are referred to as methods

JavaScript Object Literal Syntax object_name = { property1 : value1, property2 : value2, method1 : function ( parameters ) { function_body } };

Accessing Object Properties and Methods Syntax to access properties object_name. property_name ; // or object_name [" property_name "]; Syntax to access methods object_name. method_name ( parameters );

JavaScript Numeric Arrays JavaScript numeric arrays are special objects with numeric indices Array creation syntax: array_name = [ item1, item2,...]; Array access syntax: array_name [ index ]; The length property of an array stores the number of elements in an array:

Some JavaScript Array Methods tostring: converts an array to a string join: converts an array to a string with specified separator pop: removes the last element of the array push: adds a new element to the end of an array sort: sorts an array in place

JavaScript Associative Arrays JavaScript associative arrays are objects The use of named indices converts an array to an object The array methods and properties are incompatible with the object type