JavaScript Diversion. It s not just about Java

JavaScript Diversion It‟s not just about Java. A Quick Diversion You have learned fundamental features of all languages. So let‟s briefly focus on ...
Author: Laurel Davidson
4 downloads 2 Views 283KB Size
JavaScript Diversion

It‟s not just about Java.

A Quick Diversion You have learned fundamental features of all languages. So let‟s briefly focus on JavaScript. Convince you that you can do any language.  A handy tool. 

• Particularly useful for designing web pages.

Not Related to Java JavaScript is a different programming language from Java. 

Similar names that‟s all.

JavaScript developed for programming on web pages 

JavaScript is built into browsers • Netscape • IE • etc.

 

Makes running your code very easy. Graphics, file writing, (etc.) more limited than Java.

Similarities Has all the fundamental features that we‟ve discussed in Java if statements  for loops  while loops  etc. 

Most modern programming languages are similar in features.

Big Difference Big difference 

“Untyped” language   

So don‟t declare a variable type. e.g., no int, double, float, char, etc. Just say var i = 1; var j = 3.0; var k = i+j; var letter = „c‟; var message = “go skiing”;

Medium-Big Difference Variable Scope 

In Java, variables are local  



only useable within the {…} where defined e.g. if(temp == 85) { String message = “go play”; } System.out.println(message);

In JavaScript variables are usually global if(temp == 85) { var message = “go play”; } document.write(message);

error! exception is local within functions (stay tuned)

works!

Small Differences Devil in the details   

document.write() instead of System.out.println() Different input methods. Other methods differ.

Mostly annoyances. 

You know the basic logic/structure! 

Learn one (modern) language, know them all.

HTML Hypertext Markup Language All web pages use HTML  

Just tags to delineate text. NOT part of the programming language. Hello! I‟m going to insert some code on my web page! all opening tags have corresponding closing tags



Save this as “hello.html”, and it will display in a browser.

SCRIPT Tag Insert JavaScript with the tag.

JavaScript code!

Demo with laptop in class!

document.write("Hello!"); document.write(""); document.write("Wrote this with code!"); document.write("");

Just like System.out.println() but writes out stuff onto the web page. Try it!

Using If and Loops Same as Java document.write("Even and odd numbers to 10."); for(var i=1; i

Suggest Documents