JavaScript Introduction

Javascript Can Change HTML Content

One of many JavaScript HTML methods is getElementById().

The example below "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to "Hello JavaScript":

Example

document.getelementById ("demo").innerHTML = "Hello JavaScript"

JavaScript accepts both double and single quotes:

Example

document.getelementById ('demo').innerHTML = 'Hello JavaScript'

Javascript Can Change HTML Styles(CSS)

Changing the style of an HTML element, is a variant of changing an HTML Attribute:

Example

document.getelementById ('demo').style.fontSize = "35px";

Javascript Can Hide HTML Elements

Hiding HTML elements can be done by changing the display style:

Example

document.getelementById ('demo').style.display = "none";

Javascript Can Show HTML Elements

Showing hidden HTML elements can also be done by changing the display style:

Example

document.getelementById ('demo').style.display = "block";

Did You Know?
JavaScript and Java are completely different languages, both in concept and design.
JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.
ECMA-262 is the official name of the standard. ECMAScript is the official name of the language.

JavaScript Syntax

JavaScript Values

The JavaScript syntax defines two types of values:

  • Fixed values
  • Variable values

Fixed values are called Literals.

Variable values are called Variables.

JavaScript Literals

The two most important syntax rules for fixed values are:

1.Numbers are written with or without decimals:

Example

10.50


1001

2.Strings are text, written within double or single quotes:

Example

"John Doe"


'John Doe'

JavaScript Variables

In a programming language, variables are used to store data values.

JavaScript uses the keywords var, let, and const to declare variables.

An equal sign is used to assign values to variables.

In this example, x is defined as a variable. Then, x is assigned (given) the value 6:

Example

let x;
x = 6;

JavaScript Comments

Not all JavaScript statements are "executed".

Code after double slashes // or between /* and */ is treated as a comment.

Comments are ignored, and will not be executed:

Example

let x; // I will be executed

// x = 6; I will NOT be executed

JavaScript Operator

Javascript operators are used to perform different types of mathematical and logical computations.

Example

The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator + compares values

JavaScript Assignment

The Assignment Operator (=) assigns a value to a variable:

Assignment Example

// Assign the value 5 to x
let x = 5;
// Assign the value 2 to y
let y = 2;
// Assign the value x + y to z:
let z = x + y;

JavaScript Arithmetics Operator

Arithmetic Operators are used to perform arithmetic on numbers:

Arithmetic Operator Example

let a = 3;
let x = (100 + 50) * a;

JavaScript Assignment Operator

Assignment operators assign values to JavaScript variables.

The Addition Assignment Operator (+=) adds a value to a variable.

Assignment

let x = 10;
x += 5;