Syntax means set of rules to write a sentence. Here syntax also refers the rules to write a JavaScript program. Unless we write a program with correct syntax, it is impossible to get output.
So we must know the syntax of JavaScript program or how to write a JavaScript program.
A JavaScript program consists of keywords, variables, value, operators, expressions, comments etc.
How to assign a variable:
In a programming language variables are used to store data values. JavaScript uses let, var and const to declair variables. An equal sign(=) is used to send value to the variable.
For example:
let x;
x = 6;
(Here let is the keyword of JavaScript, x is the variable and 6 is the value)
When x will be called in the program, 6 will be displayed or if x will be used in a calculation, instead x, 6 will perform.
Try Yourself
Output
Let see another example
var x, y,
x = 4;
y = 6;
z = x + y;
Here, var is the keyword,
x, y and z are the variables ,
4 and 6 are the values.
in this program, whenever z will be called, it will display the value x+y.
(x+y = 4+6 = 10). So, 10 will be displayed. When we change the value of x or y or both, value of z will be automatically changed.
Try yourself
Output |
Keywords are the reserved words in a program, used to tell the software, what to do the data given with. In above example let and var are the keywords, telling variables x, y and z are storing the given values.
Now write a program, to store values in variables.
<html>
<body>
<h2>Variables and Values in JavaScript </h2>
<p>Keywords let and var are used to tell the variables to store the values and '=' operator is used to sent the value to a variable.</p>
<p id="demo"></p>
<script>
let x, y;
x = 5;
y = 6;
z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Try above program yourself in try it editor.
Literals
We can display /calculate direct values (numbers/text) also without any variables, like 7+8, 98*7 or "John Hockins" etc
For example:
document.getElementById("demo").innerHTML = 7+8;
document.getElementById("demo").innerHTML = 98*7;
document.getElementById("demo").innerHTML = "John Hockins";
<html>
<body>
<h2>JavaScript Literals</h2>
<p>Number can be written with or without decimals.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 99.50;
</script>
</body>
</html>
Try yourself
Output
<html>
<body>
<h2>JavaScript Literals</h2>
<p>Strings are written within inverted commas</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "John Hockins";
</script>
</body>
</html>
Try yourself
Output
Expressions:
An expression is a combination of values, variables and operators. It computes with these elements and gives a value. The process of calculation/computation is called the evaluation.
Expression with constant Value:
document.getElementById("demo").innerHTML = 98*7;
See with complete HTML codes
<html>
<body>
<h2>JavaScript Expressions</h2>
<p>Expressions with constant values.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 98 * 7;
</script>
</body>
</html>
Try yourself
Output |
Expression with values stored in variables
document.getElementById("demo").innerHTML = x*7;
<html>
<body>
<h2>JavaScript Expressions</h2>
<p>Expressions with variables.</p>
<p id="demo"></p>
<script>
var x;
x = 97;
document.getElementById("demo").innerHTML = x * 10;
</script>
</body>
</html>
Now try yourself
Output |
Expression with String:
document.getElementById("demo").innerHTML = "Peter" + " " + "samuel";