Docsity

JavaScript Programming Fundamentals: A Beginner's Guide, Study notes of Java Programming

Partial preview of the text

Download JavaScript Programming Fundamentals: A Beginner's Guide and more Study notes Java Programming in PDF only on Docsity!

JavaScript

Java vs. JavaScript

  • JavaScript is a subset of Java
  • JavaScript is simpler and less powerful than Java
  • JavaScript programs can be embedded within HTML files; Java code must be separate
  • Java code runs in an applet window, whereas JavaScript code can run directly within a web browser

Elements of JavaScript

  • A JavaScript program consists of a series of statements. Each statement (command) is a single line that indicates an action for the browser to take.
  • An object is anything - from the browser window itself to an element displayed within the document.
  • A method is a process by which JavaScript manipulates or affects the properties of an object.
  • A variable is a named item in a program that stores a data value. Variables must be declared before they are used.

JavaScript syntax

  • JavaScript is case sensitive and ignores most occurrences of extra white space.
  • You shouldn’t break a statement into several lines.
  • Comments in JavaScript are inserted with

// comment

JavaScript syntax

  • A function is a collection of commands that performs an action or returns a value.
  • Each function has a name which identifies it.
  • Parameters are values used by the function. FunctionName(parameter values)
  • A function is executed only when called by another JavaScript command.
  • For a function to return a value, it must include a return statement.

Function syntax

function FunctionName ( parameters )

{ JavaScript commands return value ; }

Writing output to an HTML file

  • To write text to a webpage with JavaScript, use the method document.write(" text "); where text is stuff to be written to the webpage.
  • text could even contain HTML commands, which will be interpreted by the browser as actual HTML code.

Very simple function

Writing HTML code with JavaScript

Function with parameters

Simple arithmetic

Anti-spam function