Menu Close

JavaScript Syntax

javascript syntax

In this article, We are going to learn about JavaScript Syntax. If you are a beginner in JavaScript, Then you have knowledge about JavaScript syntax.
In the previous tutorial, we have seen how to use JavaScript in your HTML file or project, Click Here to learn.
Before going through this article, Let’s understand what is the syntax?

What is Syntax?

The syntax is a set of rules, principles that define the structure of a program in programming languages. Every programming languages have their own syntax structure.

What is JavaScript Syntax?

JavaScript syntax is a set of rules, principles that define how use JavaScript code in a proper way or without facing any error.

Example:

// Variable declaration
var a, b, c;

//assign value
a = 12;
b = 45;

//Compute the values.
c = a + b

JavaScript Value:

JavaScript has two types of value Fixed value and variable value. Fixed value are called literals and variable value are called variable.

JavaScript Literals:

In JavaScript literals, not a name, it values itself. The two most important rules for fixed value in JavaScript.

Numbers are written with or without decimal.

12.90
12
450

String are text that can be written within single or double quotes.

"javascript"
'javascript'

JavaScript Variables:

In programming languages, Variables are just like a container that is used to store the values. In JavaScript, To declare the variable use the var keyword.

JavaScript Identifiers:

JavaScript identifiers is the names which are represent the variables names.
To create the identifiers in JavaScript, You have to follow some rules.

  • The identifier does not start with digits.
  • The identifier starts with letters ( Uppercase or Lowercase ).
  • Identifier also start with underscore ( _ ) and Doller sign ( $ ).
  • An identifier can contain Alphabet, Digits, Underscore, and Doller sign.

Valid Identifier:

var name;
var roll_no;
var address12;

Invalid Identifier:

var 12name;

JavaScript Expressions:

An expression is a combination of variables, values, and operators which compute a value. The combination is called evaluation.

Example

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>JavaScript Statement</h2>
<p id="myID1"></p>
<p id="myID2"></p>

<script>
//Declare variable 
var x, result;
x = 100;
result = x * 10;
document.getElementById("myID1").innerHTML = result;
document.getElementById("myID1").innerHTML = 5 * 3;;
</script>
</body>
</html>

In above example x * 10 and 5* 3 represent the expression.

JavaScript Keywords:

In JavaScript, the JavaScript keyword is used to identify action to be performed.
The var keyword tells the browser to create the variable. JavaScript keywords are the reserved words in JavaScript.

Example:

 var x, y, z;
 x = 30;
 y = 56
 z = x + y;

JavaScript Operators:

JavaScript support various operators ( +, -, %, *, / ) to compute value.

Example:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>JavaScript Operators</h2>
<script>
alert(12 + 20 * 2 );
</script>
</body>
</html>

JavaScript Comments:

In JavaScript, not all the statements are executed. statement written after double slash ( // ) or writtent between /* and */ are treated as comment.

Example

var x = 'JavaScript'; // Executed
// var y = 12;    Not Executed

JavaScript is Case Sensitive:

All the JavaScript identifiers are case sensitive.
In JavaScript, the name firstname and FirstName are the two different variables.

Example

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>JavaScript Statement</h2>
<p id="myID1"></p>
<script>
//Declare variable 
var firstname, firstName;
firstname = 'Vishvajit'
firstName = ' Rao';
document.getElementById("myID1").innerHTML = firstname + firstName;
</script>
</body>
</html>

Conclusion:

In this tutorial, You have learned about JavaScript Syntax. JavaScript syntax is set rules, protocols that define how to use JavaScript in a proper way.

I hope this tutorial will help you. If you like this article, please comment and share it with your friends who want to learn JavaScript language from scratch to advanced.

Fore More Information:- Click Here

Use Javascript
JavaScript Comments

Related Posts