JavaScript

JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.
One of many JavaScript HTML methods is getElementById() .
JavaScript can change HTML attribute values.


In this example JavaScript changes the value of the src (source) attribute of an image:

<button onclick="document.getElementById('myImage').src='img/pic_bulbon.gif'">Turn on the light</button>
<img id="myImage" src="img/pic_bulboff.gif" style="width:100px">
<button onclick="document.getElementById('myImage').src='img/pic_bulboff.gif'">Turn off the light</button>







JavaScript can change the style of an HTML element.

<p id="txt_size"> JavaScript can change the style of an HTML element. </p>
<button type="button" onclick="document.getElementById('txt_size').style.fontSize='8px'"> 8 px </button>
<button type="button" onclick="document.getElementById('txt_size').style.fontSize='16px'"> 16 px </button>
<button type="button" onclick="document.getElementById('txt_size').style.fontSize='32px'"> 32 px </button>
<button type="button" onclick="document.getElementById('txt_size').style.fontSize='64px'"> 64 px </button>







JavaScript can hide / show HTML elements.

<p id="hide"> JavaScript can hide / show HTML elements. </p>
<button type="button" onclick="document.getElementById('hide').style.display='none'"> hide </button>
<button type="button" onclick="document.getElementById('hide').style.display='block'"> show </button>





In HTML, JavaScript code is inserted between <script> & </script> tags.
Scripts can be placed in the body, or in the head section of an HTML page, or in both.

Scripts can also be placed in an external file with extension .js with the code: <script src="myScript.js"> </script>

Placing scripts in external files has some advantages:



JavaScript Functions

A JavaScript function is a block of JavaScript code, that can be executed when "called" for.
For example, a function can be called when an event occurs, like when the user clicks a button.



<script>
    function myFunction_001() {document.getElementById("demo").innerHTML = "you pressed the button function 1";}
    function myFunction_002() {document.getElementById("demo").innerHTML = "you pressed the button function 2";}
    function myFunction_003() {document.getElementById("demo").innerHTML = "TEST THESE FUNCTIONS";}
</script>

<p id="demo">TEST THESE FUNCTIONS </p>

<button type="button" onclick="myFunction_001()"> function 1 </button>
<button type="button" onclick="myFunction_002()"> function 2 </button>
<button type="button" onclick="myFunction_003()"> reset </button>

TEST THESE FUNCTIONS





JavaScript can "display" data in different ways:



JavaScript Programs

A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements .
JavaScript statements are composed of values, operators, expressions, keywords, and comments.
Semicolons separate JavaScript statements.
Ending statements with semicolon is not required, but highly recommended.
The statements are executed, one by one, in the same order as they are written.
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.
A JavaScript program is a list of programming statements to be executed by a computer.

<p id="tot"> </p>

<script>
    var x, y, z; // Statement 1
    x = 5; // Statement 2
    y = 6; // Statement 3
    z = x + y; // Statement 4
    document.getElementById("tot").innerHTML = "The value of z is " + z + ".";
</script>


JavaScript Code Blocks

JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, is in JavaScript functions


JavaScript Keywords

JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.

Here is a list of some of the keywords :
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
function Declares a function
if ... else Marks a block of statements to be executed, depending on a condition
return Exits a function
switch Marks a block of statements to be executed, depending on different cases
try ... catch Implements error handling to a block of statements
var Declares a variable
etc...


JavaScript Syntax

JavaScript uses the Unicode character set.

JavaScript syntax is the set of rules, how JavaScript programs are constructed.

The JavaScript syntax defines two types of values:


JavaScript uses arithmetic operators to compute values:
JavaScript uses an assignment operator ( = ) to assign values to variables
Operator Example Same As
= 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
**= x **= y x = x ** y

The "equal to" operator is written like == in JavaScript.


The comparison operators:
The logical operators:
An expression is a combination of values, variables, and operators, which computes to a value.
The computation is called an evaluation. For example, 5 * 10 evaluates to 50.

The values can be of various types, such as numbers and strings.
For example, "Mario" + " " + "Rossi", evaluates to "Mario Rossi"

Identifiers are names used to name variables, and keywords, functions and labels.
In JavaScript, the first character must be a letter, or an underscore (_), or a dollar sign ($).
Subsequent characters may be letters, digits, underscores, or dollar signs.

All JavaScript identifiers are case sensitive.

Hyphens (like first-name) are not allowed in JavaScript. They are reserved for subtractions.
JavaScript programmers tend to use camel case that starts with a lowercase letter (like firstName)

JavaScript arrays are written with square brackets.
var cars = ["Saab", "Volvo", "BMW"];

JavaScript objects are written with curly braces {}.
Object properties are written as name:value pairs, separated by commas.
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};