JavaScript Syntax

Back to JS page

Table of Contents

JavaScript Values

The JavaScript syntax defines two types of values:

JavaScript Literals

The most important syntax rules for literals (fixed values) are:

Numbers are written with or without decimals:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Numbers</h4>
<p>Number can be written with or without decimals:</p>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
document.getElementById("demo1").innerHTML = 10.50;
document.getElementById("demo2").innerHTML = 1001;
</script>

</body>
</html>

Strings are text, written within double or single quotes:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Strings</h4>
<p>Strings can be written with double or single quotes:</p>
<p id="demo3"></p>
<p id="demo4"></p>

<script>
document.getElementById("demo3").innerHTML = "John Doe";
document.getElementById("demo4").innerHTML = 'John Doe';
</script>

</body>
</html>

Example 1

Result View Example

JavaScript Variables

Variables are containers for storing data values.

Variables must be identified with unique names.

The let and const keywords create variables:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Variables</h4>
<p id="demo"></p>

<script>
let x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Example 2

Result View Example

JavaScript Identifiers

An identifier is the name given to a variable.

Rules for identifiers:

JavaScript Operators

JavaScript assignment operators (=) assign values to variables:

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Operators</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
let a, b, c;
a = 5;
b = 2;
c = a + b;
document.getElementById("demo1").innerHTML = "Value of a + b = " + c;
document.getElementById("demo2").innerHTML = "Value of a * b = " + (a * b);
</script>

</body>
</html>

Example 3

Result View Example

JavaScript Expressions

An expression is a combination of values, variables, and operators, which computes to a value:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Expressions</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>

<script>
document.getElementById("demo1").innerHTML = (5 + 6) * 10;
let x = 5;
document.getElementById("demo2").innerHTML = x * 10;
document.getElementById("demo3").innerHTML = "John" + " " + "Doe";
</script>

</body>
</html>

Example 4

Result View Example

JavaScript is Case Sensitive

JavaScript identifiers are case sensitive.

The variables lastName and lastname, are different variables:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Syntax</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript is Case Sensitive</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
let lastName = "Doe";
let lastname = "Smith";
document.getElementById("demo1").innerHTML = lastName;
document.getElementById("demo2").innerHTML = lastname;
</script>

</body>
</html>

Example 5

Result View Example

JavaScript and Camel Case

Historically, programmers have used different ways of joining multiple words into one variable name:

JavaScript programmers tend to use lower camel case.

Document

Document in project

You can Download PDF file.

Reference