DOC PREVIEW
Penn CIT 597 - Language Fundamentals

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JavaScript Language Fundamentals Jan 13 2019 About JavaScript JavaScript is not Java or even related to Java 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 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 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 2 Using JavaScript in a browser JavaScript code is included within script tags script type text javascript document write h1 Hello World h1 script 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 h1 Hello World h1 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 3 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 script type text javascript document write Hello World script 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 noscript message noscript to display a message in place of whatever the JavaScript would put there 4 Where to put JavaScript JavaScript can be put in the head or in the body of an HTML document JavaScript functions can be put in a separate js file JavaScript functions should be defined in the head This ensures that the function is loaded before it is needed JavaScript in the body will be executed as the page loads script src myJavaScriptFile js script Put this in the head An external js file lets you use the same JavaScript on multiple HTML pages The external js file cannot itself contain a script 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 5 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 Strings may be enclosed in single quotes or double quotes Since you can t be sure avoid octal altogether 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 6 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 local to that function accessible only within that function Variables declared outside a function are global accessible from anywhere on the page 7 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 8 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 9 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 10 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 11 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 12 JavaScript is not Java By now you should have realized that you already know a great deal of JavaScript JavaScript has some features that resemble features in Java So far we have talked about things that are the same as 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 13 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 14 Exception handling II try statements to try catch e if test1 exception handling for the case that test1 is true catch e if test2 exception handling for when test1 is false and test2 is true catch e exception handling for when both test1and test2 are false finally optional as usual code that is always executed Typically the test would be something like e InvalidNameException 15 Object literals You don t declare the types of variables in JavaScript JavaScript has object literals written with


View Full Document

Penn CIT 597 - Language Fundamentals

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Language Fundamentals
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Language Fundamentals and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Language Fundamentals and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?