Showing posts with label Syntax in JavaScript. Show all posts
Showing posts with label Syntax in JavaScript. Show all posts

JavaScript: Syntax

 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 Try it yourself editor

 
Code
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 Try it yourself editor
 
Code
Output

Keywords:

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";

Now see the complete coding with HTML

<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 Try it yourself editor

 
Code
Output

For strings (text):

<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 Try it yourself editor

 
Code
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


Try it yourself editor
 
Code
Output

Expression with values stored in variables

document.getElementById("demo").innerHTML = x*7;

See complete coding with HTML

<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


Try it yourself editor
 
Code
Output

Expression with String:

document.getElementById("demo").innerHTML = "Peter" + " " + "samuel";

See complete coding with HTML
<html>
<body>
<h2>JavaScript Expressions</h2>
<p>Expressions in String Value.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Peter" + " "  + "Samuel";
</script>
</body>
</html>
Try the above code in Try it editor.

JavaScript Comments:
The JavaScript comments are written with double slashes // or between /* and */ are not executed when program runs. Single line comment is written with // and multiple lines comments are written between /* and */.
For example:
<html>
<body>
<h2>JavaScript Comments are NOT Executed</h2>
<p id="demo"></p>
<script>
let x;
x = 95;
// x has a numeric value 95
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
When we execute the above program, comment written with // 'x has a numeric value 95' will not be executed.
Try the above code in Try it editor.

Case Sensitive:
Unlike HTML, JavaScript is case sensitive.
For this firstname and firstName are two different variables.
For example:
<html>
<body>
<h2>JavaScript is Case Sensitive</h2>
<p>Try to change firstName to firstname.</p>
<p id="demo"></p>
<script>
let firstname, firstName;
firstName = "Peter";
firstname = "Anderson";
document.getElementById("demo").innerHTML = firstName;
</script>
</body>
</html>
Run this code, result will display the value 'Peter' stored in 'firstName' not 'firstname' .

Camel Case:
In programming multiple words are joined to make one variablename, the words can be joned by using 
Underscore (First_Name, first_name, first_Name etc)
Upper camel case (FirstName)
Lower camel case (firstName)
TOP

Python: Functions