JS Syntax

Java Script syntax is the set of rules. Java Script can be implemented using Java Script statements that are placed within the <script>... </script> HTML tags in a web page.

We can place the <script> tags, containing our Java Script, anywhere within our web page, but it is normally recommended that we should keep it within the <head> tags.

The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.

Java Script programs are constructed as follows:

 
var x, y;       // How to declare variables
x = 5; y = 6;    // How to assign values
z = x + y;     // How to compute values

 

The Java Script syntax defines two types of values: Fixed values and Variable values. Fixed values are called literals. Variable values are called variables.

The most important rules for writing fixed values are: Numbers are written with or without decimals:

 
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Numbers</h2>
<p>Number can be written with or without decimals.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 15.50;
</script>

</body>
</html>

Output:

Java Script Numbers

Number can be written with or without decimals.

 

 

We can written Strings (are text) within double or single quotes:

Example:

 
<!DOCTYPE html>
<html>
<body>

<h2>Java Script Strings</h2>
<p>Strings can be written with double or single quotes.</p>
<p id="demo2"></p>
<script>
document.getElementById("demo").innerHTML ="'UK Academe'";
</script>

</body>
</html>

Output:

Java Script Strings

Strings can be written with double or single quotes.

 

Like many other programming languages, Java Script has variables. Variables can be thought of as named containers. We can put data in these containers and simply name the container to refer to the data.

Before we use a variable in a Java Script program, we must declare it. Java Script uses the var keyword to declare variables. To assign values to variables, an equal sign is used.

x is defined as a variable in this example. Then x (given) is assigned to 7:

Example:


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>
<p>In this example, x is defined as a variable. Then, x is assigned the value of 7:</p>
<p id="demo"></p>
<script>
var x;
x = 7;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output:

Java Script Variables

In this example, x is defined as a variable. Then, x is assigned the value of 7:

 

Java Script uses arithmetic operators ( + - * / ) to compute values:

Example:


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Operators</h2>
<p>JavaScript uses arithmetic operators to compute values (just like algebra).</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = (4 + 6) * 10;
</script>

</body>
</html>

 

Output:

JavaScript Operators

JavaScript uses arithmetic operators to compute values (just like algebra).

 

 

JavaScript uses an assignment operator ( = ) to assign values to variables:

Example:

 
<!DOCTYPE html>
<html>
<body>

<h2>Assigning JavaScript Values</h2>
<p>In JavaScript the = operator is used to assign values to variables.</p>
<p id="demo"></p>
<script>
var x, y;
x = 4;
y = 6;
document.getElementById("demo").innerHTML = x + y;
</script>

</body>
</html>

Output:

Assigning JavaScript Values

In JavaScript the = operator is used to assign values to variables.

 

An expression is a combination of values, variables, and operators, which computes to a value. The computation is called an evaluation.

For example, 6 * 10 evaluates to 60:

Example:


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Expressions</h2>
<p>Expressions compute to values.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 * 10;
</script>

</body>
</html>

Output:

JavaScript Expressions

Expressions compute to values.

 

 

Expressions can also contain variable values:

Example:

 
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Expressions</h2>
<p>Expressions compute to values.</p>
<p id="demo"></p>
<script>
var x;
x = 6;
document.getElementById("demo").innerHTML = x * 10;
</script>

</body>
</html>

Output:

JavaScript Expressions

Expressions compute to values.

 

 

The values of the numbers and strings can be different types. For instance, "UK" + " "+ "Academy", "UK Academy" evaluates:

Example:

 
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Expressions</h2>
<p>Expressions compute to values.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "UK" + " " + "Academe";
</script>

</body>
</html>

Output:

JavaScript Expressions

Expressions compute to values.

 

To identify actions to be performed, JavaScript keywords are used. The var keyword tells the browser to create variables:

Example:


<!DOCTYPE html>
<html>
<body>
<h2>The var Keyword Creates Variables</h2>
<p id="demo"></p>
<script>
var x, y;
x = 4 + 6;
y = x * 10;
document.getElementById("demo").innerHTML = y;
</script>

</body>
</html>
 

Output:

The var Keyword Creates Variables

 

Not all JavaScript statements are "executed". The code is considered as a comment after the double slashes // or from / * to * /. Comments will be ignored and not carried out:

Example:


<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comments are NOT Executed</h2>
<p id="demo"></p>
<script>
var x;
x = 4;
// x = 10; I will not be executed
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output:

JavaScript Comments are NOT Executed

 

Names are identificators. In JavaScript, Identifiers (and keywords, functions and labels) are used to name variables. In most programming languages, the rules for legal names are similar. A letter, or an underscore(_), or a dollar sign ($), shall be the first character in JavaScript. Please, letters, digits, underscores, or dollar signs may be the following characteristics.

Note: As the first character number is not allowed, JavaScript can easily distinguish identifiers from numbers.

 

All JavaScript identifiers are case sensitive. The variables lastName and lastname, are two different variables:

Example:

 
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript is Case Sensitive</h2>
<p>Try change lastName to lastname.</p>
<p id="demo"></p>
<script>
var lastname, lastName;
lastName = "UK";
lastname = "Academe";
document.getElementById("demo").innerHTML = lastName; </script> </body> </html>

Output:

JavaScript is Case Sensitive

Try change lastName to lastname.

 

Note: JavaScript does not interpret VAR or Var as the keyword var.

 

Programmers have historically used several ways to connect multiple words to one variable name:

Hyphens: first-name, last-name, master-card, inter-city.

Note: Hyphens are not allowed in JavaScript. They are reserved for subtractions.

Underscore: first_name, last_name, master_card, inter_city.

Upper Camel Case (Pascal Case): FirstName, LastName, MasterCard, InterCity.

Lower Camel Case:

Programmers in JavaScript tend to have the camel case starting with an inferior letter: firstName, lastName, masterCard, interCity.

 

JavaScript uses the Unicode character set. Unicode (almost) encompasses all the world's signs, punchs, and symbols.