Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Intro
JS Primitive Data
JS Complex Data
JSON
Action Plan
JavaScript and Java are completely different languages,
both in concept and design.
JavaScript Origin
JavaScript is sometimes also
is called HTML Dog
Year Name Description
JavaScript Versions
JavaScript
JSON
AJAX
VBScript
Tehnologii de Prelucrare a
datelor direct n browser
abstract arguments await* boolean
Example
document.getElementById("demo").innerHTML = "Hello
JavaScript";
JavaScript in <body>
<body>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";}
</script>
</body>
External JavaScript
<script src="myScript.js"></script>
JavaScript Syntax
Keyword Description
break Terminates a switch or a loop
continue Jumps out of a loop and starts at the top
debugger Stops the execution of JavaScript, and calls (if available) the debugging
function
do ... while Executes a block of statements, and repeats the block, while a condition is
true
for Marks a block of statements to be executed, as long as a condition is true
JavaScript Keywords
JavaScript comments can be used to explain JavaScript code, and to
make it more readable.
// Change heading: or
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
JavaScript Comments
JavaScript variables are containers for storing data values.
All JavaScript variables must be identified with unique
names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive
names (age, sum, totalVolume).
The general rules for constructing names for variables (unique
identifiers) are:
Names can contain letters, digits, underscores, and dollar signs.
Names must begin with a letter
Names can also begin with $ and _ (but we will not use it in this
tutorial)
Names are case sensitive (y and Y are different variables)
Reserved words (like JavaScript keywords) cannot be used as
names
To assign a value to the variable, use the equal sign
JavaScript Variables
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
JavaScript Operators
Primitive Data
A primitive data value is a single simple data value with no additional
properties and methods.
The typeof operator can return one of these primitive types:
string
number - JavaScript has only one type of numbers
boolean
null
Undefined - var person;
Complex Data
The typeof operator can return one of two complex types:
function
function myFunction();
object
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
JavaScript Functions
In JavaScript, objects and functions are also variables.
In JavaScript, scope is the set of variables, objects, and
functions you have access to.
JavaScript has function scope: The scope changes inside
functions.
A variable declared outside a function,becomes GLOBAL.
A global variable has global scope: All scripts and
functions on a web page can access it.
JavaScript Scope
HTML events are "things" that happen to HTML
elements.
When JavaScript is used in HTML pages, JavaScript
can "react" on these events.
Event Description
onmouseout The user moves the mouse away from an HTML element
JavaScript Events
Property Description
closed Returns a Boolean value indicating whether a window has been closed or not
document Returns the Document object for the window (See Document object)
frameElement Returns the <iframe> element in which the current window is inserted
history Returns the History object for the window (See History object)
location Returns the Location object for the window (See Location object)
name Sets or returns the name of a window
navigator Returns the Navigator object for the window (See Navigator object)
opener Returns a reference to the window that created the window
outerHeight Returns the outer height of a window, including toolbars/scrollbars
outerWidth Returns the outer width of a window, including toolbars/scrollbars
pageXOffset Returns the pixels the current document has been scrolled (horizontally) from the upper left corner of the window
pageYOffset Returns the pixels the current document has been scrolled (vertically) from the upper left corner of the window
getSelection() Returns a Selection object representing the range of text selected by the user
matchMedia() Returns a MediaQueryList object representing the specified CSS media query string
Code Outputs
\' single quote
\" double quote
\\ backslash
Code Outputs
\b Backspace
\r Carriage Return
\f Form Feed
\t Horizontal Tabulator
\v Vertical Tabulator
JavaScript Strings
length - returns the length of a string
indexOf() - method returns the index of (the position of) the first occurrence of a
specified text in a string
lastIndexOf() method returns the index of the last occurrence of a specified text
in a string
search() method searches a string for a specified value and returns the position of
the match
slice() extracts a part of a string and returns the extracted part in a new string
substring() is similar to slice()
substr() is similar to slice()
replace() method replaces a specified value with another value in a string
toUpperCase() and toLowerCase() - Converting to Upper and Lower Case
concat() joins two or more string
The charAt() method returns the character at a specified index (position) in a
string
The charCodeAt() method returns the unicode of the character at a specified
index in a string
A string can be converted to an array with the split() method
Number Properties
Method Description
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
JS Math
Type Example
Method Description
JS Date
An array is a special variable, which can hold more than one
value at a time.
If you have a list of items (a list of car names, for example),
storing the cars in single variables could look like this:
var car1 = "Saab";
var car2 = "Volvo";
var car3 = "BMW";
However, what if you want to loop through the cars and find a
specific one? And what if you had not 3 cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you
can access the values by referring to an index number.
Syntax:
var array_name = [item1, item2, ...];
Example
var cars = ["Saab", "Volvo", "BMW"];
JS Arrays
Array Example: var fruits = ["Banana", "Orange","Apple", "Mango"];
toString()
Example:
document.getElementById("demo").innerHTML = fruits.toString();
Result : Banana, Orange, Apple, Mango
join()
Example:
document.getElementById("demo").innerHTML = fruits.join(" * ");
Result : Banana * Orange * Apple * Mango
Popping()
Example:
fruits.pop(); // Removes the last element ("Mango") from fruits
Result : Banana, Orange, Apple
Pushing()
Example:
fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits
Result : Banana, Orange, Apple, Mango, Kiwi
And others like shift, unshift, delete, splice, concat, slice, max, min, sorting
Conditional Statements
for - loops through a block of code a number of times
for (i = 0; i < cars.length; i++) { instructions;}
for/in - loops through the properties of an object
for (x in person) { instructions;}
while - loops through a block of code while a specified
condition is true
while (i < 10) { instructions;}
do/while - also loops through a block of code while a
specified condition is true
do { instructions;} while (i < 10);
When you search for data in a text, you can use this
search pattern to describe what you are searching for.
Regular Expression
Brackets are used to find a range of characters:
Expression Description
The finally statement lets you execute code, after try and catch,
regardless of the result.
Example
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Block of code to be executed regardless of the try / catch result
}
JS Errors
Code Debugging
Programming code might contain syntax errors, or logical errors.
Many of these errors are difficult to diagnose.
Often, when programming code contains errors, nothing will happen. There are no
error messages, and you will get no indications where to search for errors.
Searching for (and fixing) errors in programming code is called code debugging.
JavaScript Debuggers
Debugging is not easy. But fortunately, all modern browsers have a built-in
JavaScript debugger.
Built-in debuggers can be turned on and off, forcing errors to be reported to the user.
With a debugger, you can also set breakpoints (places where code execution can be
stopped), and examine variables while the code is executing.
Normally, otherwise follow the steps at the bottom of this page, you activate
debugging in your browser with the F12 key, and select "Console" in the debugger
menu.
Methods of debugging
The console.log() Method
debugger;
JavaScript Debugging
JSON stands for JavaScript Object Notation
What is JS JSON [ I ]
JSON Syntax Rules
Data is in name/value pairs
Data is separated by commas
Curly braces hold objects
Square brackets hold arrays
</script>
THE END