JavaScript Syntax
JavaScript Syntax
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
document.getElementById("demo").innerHTML = "My First JavaScript";
JavaScript Values
The JavaScript syntax defines two types of values:
- Fixed Values
- Variables Values
Fixed values are called Literals.
Variable values are called Variables.
JavaScript Variables
In a programming language, variables are used to store data values. JavaScript uses the var keyword 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:
a = 5; b = 6; c = a + b;var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
JavaScript Operators
JavaScript uses arithmetic operators ( + - * / ) to compute values. JavaScript uses an assignment operator ( = ) to assign values to variables:
(5 + 6) * 10
JavaScript Expressions
An expression is a combination of values, variables, and operators, which computes to a value. The computation is called an evaluation.
Expressions can also contain variable values.The values can be of various types, such as numbers and strings.
x * 10
JavaScript Keywords
You can place an external script reference in <head> or <body> as you like. The script will behave as if it was located exactly where the <script> tag is located.
External scripts cannot contain <script> tags.
External JavaScript Advantages
Placing scripts in external files has some advantages:
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- it enable us to add several script files to one page - use several script tags:
- Cached JavaScript files can speed up page loads
<script src="myScript1.js.js"> </script>
<script src="myScript2.js.js"> </script>
External References
External scripts can be referenced with a full URL or with a path relative to the current web page.
<script src="https://www.w3schools.com/js/myScript1.js"> </script>