Docsity

Introduzione a JavaScript: Variabili, Operatori ed Eventi, Appunti di Algoritmi E Programmazione Avanzata

Anteprima parziale del testo

Scarica Introduzione a JavaScript: Variabili, Operatori ed Eventi e più Appunti in PDF di Algoritmi E Programmazione Avanzata solo su Docsity!

JavaScript Tutorial

Table of Contents

  • Introduction
    • Introduction
    • Internal JavaScript Code
    • Functions
    • External JavaScript Code

Introduction

JavaScript is a dynamic language that executes within a browser. JavaScript code is embedded within an HTML page using the JavaScript tag. The

The body

The Document write method displays the text.

Notice that the JavaScript code is enclosed in HTML comment tags:

These are often used to surround JavaScript code. In older browsers JavaScript was not recognized or handled. To avoid the display of this code in a page, the browser would ignore the contents of the comment. However, in a browser that supports JavaScript the comments tags are ignored and the code is executed.

The output will be the same.

Functions

A function consists of the function keyword followed by the name of the function , a set of open and close parentheses enclosing an optional parameter list and a body enclosed in a set of curly braces.

function functionName(parameterList) { // body }

A function uses the return keyword to return a value from a function.

JavaScript Example

Parameters are separated by commas in the function declaration.

External JavaScript Code

It is advantageous to group common functions in an external JavaScript file. This permits the reuse of the functions in the file in multiple HTML pages.

JavaScript functions are stored in a file using the .js extension. If we placed the following functions in a file named scripts.js we can reference and subsequently use the functions from an HTML page.

// functions.js function getHeader() { return "Main Heading" }

function multiply(num1, num2) { return num1*num2; }

Notice that the C++ style comment can be used in JavaScript. Also notice that the

JavaScript Language Elements

It is useful to discuss JavaScript in terms of language elements including:

Variables Operators Expressions Statements Objects Functions and methods

Variables

Variables are used to hold data. A JavaScript identifier:

Starts with a letter or underscore, and Is followed by letters, underscore or digits

JavaScript is a case-sensitive language

Scope

The scope of an identifier is either

Global – An identifier that is accessible anywhere on the page Local – Is accessible only within the function it is declared within

A global variable is typically declared simply by assigning a value to it.

globalVariable = 100;

A local variable is declared within a function using the var keyword.

function someFunction() { var counter = 0; globalVariable = 100; ... }

The identifier, counter , is local to the function and can only be used in that function. However, the identifier, globalVariable , is not preceded by the var keyword and is thus a global variable that can be used anywhere on the page, inside or outside of the function.

Literals

Literals are simple constants such as:

“frog beaks” „/nTitle/n‟ true

For string, escape sequence can be used to embed special values. An escape sequence consists of the back slash character followed by a character that has special meaning. Escape sequences recognized by JavaScript include:

Character Meaning \b backspace \f form feed

\n new line \r carriage return \t tab

\ backslash character " double quote \‟ Single quote

\ddd Octal number \xdd Tow digit hexadecimal number \xdddd Four digit hexadecimal number

Operators

The JavaScript operators include:

Precedence Operator Associativity Meaning

1 member Left-to-right.

[] new Right-to-left new

2 function call Left-to-right ()

3 ++ n/a Increment by 1

-- Decrement by 1

4! Right-to-left logical not

~ bitwise not

  • unary plus
  • unary minus

typeof type of void void delete delete

5 * Left-to-right Multiplication

/ Division % Modulo division

6 + Left-to-right addition

  • subtraction

7 << Left-to-right shift left

>> shift right

>>> arithmetic shift right

8 > Left-to-right Greater than

>= Greater than or equal < Less than

<= Less than or equal

Arrays

Arrays are allocated using the new keyword.

names = new Array(10); numbers = new Array(5);

Array indexes start at 0 and extend to the size of the array minus 1. To assign a value to an element of an array open and close brackets are used.

names[0] = "Rabbit"; names[1] = "Happy"; names[9] = "Dover";

The size of an array can be increased dynamically by assigning a value to an element pass the end of the array. Array can be created that initially has no elements at all. In addition, they are not of a fixed size but can grow dynamically.

pictures = new Array(); pictures[35] = "Mona Lisa";

The array, pictures, initially has no elements. After "Mona Lisa" has been assigned the array has 36 elements. The unassigned elements are set to Undefined.

The length property of arrays returns the number of elements in the array.

JavaScript Example

pictures = new Array(); pictures[35] = "Mona Lisa"; document.write("pictures[35] - " + pictures[35] ); document.write("pictures[30] - " + pictures[30] ); document.write("pictures.length - " +pictures.length); //-->