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.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

JavaScript Language Fundamentals. About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript”
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
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.
18-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
24-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
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.
JavaScript, Third Edition
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
12-Aug-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
Chapter 3 : Processing on the Front End JavaScript Technically its name is ECMA-262, which refers to the international standard which defines it. The standard.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Week 9 PHP Cookies and Session Introduction to JavaScript.
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.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Using Client-Side Scripts to Enhance Web Applications 1.
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.
27-Oct-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
15-Nov-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Applications Development
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Tharith Sriv.  JavaScript is a simple programming language that can be written directly into HTML documents to allow for increased interactivity with.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript, Fourth Edition
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
JavaScript is an object-based scripting language that is lightweight and cross-platform. 3-Feb-16 JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Overview JavaScript. Overview 2 About Basics Objects DOM.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript” –The name was changed when Java became.
Java Script. introduction Today’s web sites need to go much beyond HTML. browsing through a web site, to actually interact with the web site. The web.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
6-Jun-16 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Java Script Introduction. Java Script Introduction.
Scope, Objects, Strings, Numbers
Chapter 19 JavaScript.
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
PHP.
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
PHP an introduction.
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

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 “LiveScript” The name was changed when Java became popular Now that Microsoft no longer likes Java, its name for their JavaScript dialect is “Active Script” Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C language JavaScript should be fairly easy for Java programmers to learn However, JavaScript is a complete, full-featured, complex language JavaScript is seldom used to write complete “programs” Instead, small bits of JavaScript are used to add functionality to HTML pages JavaScript is often used in conjunction with HTML “forms” JavaScript is reasonably platform-independent

3 Using JavaScript in a browser JavaScript code is included within tags: document.write(" Hello World! ") ; Notes: The type attribute is to allow you to use other scripting languages (but JavaScript is the default) This simple code does the same thing as just putting Hello World! in the same place in the HTML document The semicolon at the end of the JavaScript statement is optional You need semicolons if you put two or more statements on the same line It’s probably a good idea to keep using semicolons

4 JavaScript isn’t always available Some old browsers do not recognize script tags These browsers will ignore the script tags but will display the included JavaScript To get old browsers to ignore the whole thing, use: The <!-- introduces an HTML comment To get JavaScript to ignore the HTML close comment, -->, the // starts a JavaScript comment, which extends to the end of the line Some users turn off JavaScript Use the message to display a message in place of whatever the JavaScript would put there

5 Where to put JavaScript JavaScript can be put in the or in the of an HTML document JavaScript functions should be defined in the This ensures that the function is loaded before it is needed JavaScript in the will be executed as the page loads JavaScript functions can be put in a separate.js file Put this in the An external.js file lets you use the same JavaScript on multiple HTML pages The external.js file cannot itself contain a tag JavaScript can be put in an HTML form object, such as a button This JavaScript will be executed when the form object is used

6 Primitive data types JavaScript has three “primitive” types: number, string, and boolean Everything else is an object Numbers are always stored as floating-point values Hexadecimal numbers begin with 0x Some platforms treat 0123 as octal, others treat it as decimal Since you can’t be sure, avoid octal altogether! Strings may be enclosed in single quotes or double quotes Strings can contains \n (newline), \" (double quote), etc. Booleans are either true or false 0, "0", empty strings, undefined, null, and NaN are false, other values are true

7 Variables Variables are declared with a var statement: var pi = , x, y, name = "Dr. Dave" ; Variables names must begin with a letter or underscore Variable names are case-sensitive Variables are untyped (they can hold values of any type) The word var is optional (but it’s good style to use it) Variables declared within a function are local to that function (accessible only within that function) Variables declared outside a function are global (accessible from anywhere on the page)

8 Operators, I Because most JavaScript syntax is borrowed from C (and is therefore just like Java), we won’t spend much time on it Arithmetic operators (all numbers are floating-point): + - * / % Comparison operators: = > Logical operators: && || ! (&& and || are short-circuit operators) Bitwise operators: & | ^ ~ > >>> Assignment operators: += -= *= /= %= >= >>>= &= ^= |=

9 Operators, II String operator: + The conditional operator: condition ? value_if_true : value_if_false Special equality tests: == and != try to convert their operands to the same type before performing the test === and !== consider their operands unequal if they are of different types Additional operators (to be discussed): new typeof void delete

10 Comments Comments are as in C or Java: Between // and the end of the line Between /* and */ Java’s javadoc comments, /**... */, are treated just the same as /*... */ comments; they have no special meaning in JavaScript

11 Statements, I Most JavaScript statements are also borrowed from C Assignment: greeting = "Hello, " + name; Compound statement: { statement ;...; statement } If statements: if ( condition ) statement ; if ( condition) statement ; else statement ; Familiar loop statements: while ( condition ) statement ; do statement while ( condition ); for ( initialization ; condition ; increment ) statement ;

12 Statements, II The switch statement: switch ( expression) { case label : statement ; break; case label : statement ; break;... default : statement ; } Other familiar statements: break; continue; The empty statement, as in ;; or { }

13 JavaScript is not Java By now you should have realized that you already know a great deal of JavaScript So far we have talked about things that are the same as in Java JavaScript has some features that resemble features in Java: JavaScript has Objects and primitive data types JavaScript has qualified names; for example, document.write("Hello World"); JavaScript has Events and event handlers Exception handling in JavaScript is almost the same as in Java JavaScript has some features unlike anything in Java: Variable names are untyped: the type of a variable depends on the value it is currently holding Objects and arrays are defined in quite a different way JavaScript has with statements and a new kind of for statement

14 Object literals You don’t declare the types of variables in JavaScript JavaScript has object literals, written with this syntax: { name1 : value1,..., nameN : valueN } Example (from Netscape’s documentation): car = {myCar: "Saturn", 7: "Mazda", getCar: CarTypes("Honda"), special: Sales} The fields are myCar, getCar, 7 (this is a legal field name), and special "Saturn" and "Mazda" are Strings CarTypes is a function call Sales is a variable you defined earlier Example use: document.write("I own a " + car.myCar);

15 Three ways to create an object You can use an object literal: var course = { number: "CIT597", teacher: "Dr. Dave" } You can use new to create a “blank” object, and add fields to it later: var course = new Object(); course.number = "CIT597"; course.teacher = "Dr. Dave"; You can write and use a constructor: function Course(n, t) { // best placed in this.number = n; // keyword "this" is required, not optional this.teacher = t; } var course = new Course("CIT597", "Dr. Dave");

16 Functions Functions should be defined in the of an HTML page, to ensure that they are loaded first The syntax for defining a function is: function name ( arg1, …, argN ) { statements } The function may contain return value ; statements Any variables declared within the function are local to it The syntax for calling a function is just name ( arg1, …, argN ) Simple parameters are passed by value, objects are passed by reference

17 Warnings JavaScript is a big, complex language We’ve only scratched the surface It’s easy to get started in JavaScript, but if you need to use it heavily, plan to invest time in learning it well Write and test your programs a little bit at a time JavaScript is not totally platform independent Expect different browsers to behave differently Write and test your programs a little bit at a time Browsers aren’t designed to report errors Don’t expect to get any helpful error messages Write and test your programs a little bit at a time

18 The End