JavaScript Variables

Variables are Containers for Storing Data

JavaScript Variables can be declared in 4 ways:

  • Automatically
  • Using var
  • Using let
  • Using const

In this first example, x, y, and z are undeclared variables.

They are automatically declared when first used:

Example

x = 5;
y = 6;
z = x + y;

Continue reading JavaScript Variables

JavaScript Comments

JavaScript comments can be used to explain JavaScript code, and to make it more readable.

JavaScript comments can also be used to prevent execution, when testing alternative code.

Single Line Comments

Single line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

This example uses a single-line comment before each code line:

Example

// Change heading:
document.getElementById("myH").innerHTML = "My First Page";

// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";

Continue reading JavaScript Comments

HTML Unicode (UTF-8) Reference

Unicode is a universal character set that defines all the characters needed for writing the majority of living languages in use on computers.

Unicode aims to be (and already is) a superset of all other encoded computer character sets.

The Unicode Standard covers (almost) all characters, punctuations, and symbols in the world and enables processing, storage, and transport of text independent of platform and language.

The Unicode Consortium

The Unicode Consortium develops the Unicode Standard. The goal is to replace existing character sets with UTF (Unicode Transformation Format).

The Unicode Standard is implemented in HTML, XML, JavaScript, E-mail, PHP, Databases and in all modern operating systems and browsers.


The Unicode Character Sets

Unicode can be implemented by different character sets. The most commonly used encodings are UTF-8 and UTF-16:

Charset Description
UTF-8 A variable-length character encoding (1 to 4 bytes long). UTF-8 is backwards compatible with ASCII and the preferred encoding for e-mail and web pages.
UTF-16 A variable-length character encoding. UTF-16 is used in all major operating systems like Windows, IOS, and Unix.

The first 128 characters of UTF-8 have the same binary values as ASCII, making ASCII text valid UTF-8.


The HTML Standard is Unicode UTF-8

The default character set in HTML-4 (ISO-8859-1) were limited in size and not compatible in multilingual environments.

The default character encoding in HTML-5 is UTF-8.

HTML5 pages using a different character set than UTF-8 must specify this a <meta> tag:

Example

<meta charset=”ISO-8859-1″>

Continue reading HTML Unicode (UTF-8) Reference

JavaScript Syntax

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

// How to create variables:
var x;
let y;

 

 

// How to use variables:
x = 5;
y = 6;
let z = x + y;

JavaScript Values

The JavaScript syntax defines two types of values:

  • Fixed values
  • Variable values

Fixed values are called Literals.

Variable values are called Variables. Continue reading JavaScript Syntax

JavaScript Statements

Statements

Example

let x, y, z;    // Statement 1
x = 5;          // Statement 2
y = 6;          // Statement 3
z = x + y;      // Statement 4

Continue reading JavaScript Statements

JavaScript Reserved Words

In JavaScript you cannot use these reserved words as variables, labels, or function names:

abstract arguments await* boolean
break byte case catch
char class* const* continue
debugger default delete do
double else enum* eval
export* extends* false final
finally float for function
goto if implements import*
in instanceof int interface
let* long native new
null package private protected
public return short static
super* switch synchronized this
throw throws transient true
try typeof var void
volatile while with yield

Words marked with* was new in ECMAScript 5 and ECMAScript 6.

Continue reading JavaScript Reserved Words

JavaScript Output

JavaScript Display Possibilities

JavaScript can “display” data in different ways:

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log().

Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method.

The id attribute defines the HTML element. The innerHTML property defines the HTML content:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My First Paragraph</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html>

Continue reading JavaScript Output

JavaScript Where To

The <script> Tag

In HTML, JavaScript code is inserted between <script> and </script> tags.

Example

<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>

Continue reading JavaScript Where To

JavaScript Introduction

What is JavaScript?

JavaScript is the programming language of the web.

It can update and change both HTML and CSS.

It can calculate, manipulate and validate data.

Why Study JavaScript?

JavaScript is one of the 3 languages all web developers must learn :

1. HTML to define the content of web pages

2. CSS to specify the layout of web pages

3. JavaScript to program the behavior of web pages

Continue reading JavaScript Introduction