Docsity

JavaScript: A Beginner's Guide to Understanding the Differences with Java and Fundamentals, Slides of Computer Science

Partial preview of the text

Download JavaScript: A Beginner's Guide to Understanding the Differences with Java and Fundamentals and more Slides Computer Science in PDF only on Docsity!

JavaScript

Language Fundamentals

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

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 , 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

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 can be put in a separate .js file

    • Put this HTML wherever you would put the actual JavaScript code
    • An external .js file lets you use the same JavaScript on multiple HTML pages
    • The external .js file cannot itself contain a

Variables

• Variables are declared with a var statement:

  • var pi = 3.1416, 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

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: += -= *= /= %= <<= >>= >>>= &= ^= |=

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

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 ); Docsity.com

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

Exception handling, I

  • Exception handling in JavaScript is almost the same as in Java
  • throw expression creates and throws an exception
    • The expression is the value of the exception, and can be of any type (often, it's a literal String)
  • try {

statements to try } catch ( e ) { // Notice: no type declaration for e exception handling statements } finally { // optional, as usual code that is always executed }

  • With this form, there is only one catch clause

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); Docsity.com

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");

Four ways to create an array

  • You can use an array literal: var colors = ["red", "green", "blue"];
  • You can use new Array() to create an empty array:
    • var colors = new Array();
    • You can add elements to the array later: colors[0] = "red"; colors[2] = "blue"; colors[1]="green";
  • You can use new Array( n ) with a single numeric

argument to create an array of that size

  • var colors = new Array(3);
  • You can use new Array(…) with two or more arguments

to create an array containing those values:

  • var colors = new Array("red","green", "blue");

The length of an array

  • If myArray is an array, its length is given by

myArray.length

  • Array length can be changed by assignment beyond the

current length

  • Example: var myArray = new Array(5); myArray[10] = 3;
  • Arrays are sparse, that is, space is only allocated for elements that have been assigned a value
  • Example: myArray[50000] = 3; is perfectly OK
  • But indices must be between 0 and 2 32 -
  • As in C and Java, there are no two-dimensional arrays; but you can have an array of arrays: myArray[5][3]