JavaScript Syntax
JavaScript Syntax
JavaScript syntax comprises a set of rules that define how to construct a JavaScript code.
JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
You can place the <script> tags, containing your JavaScript, anywhere within your web page,
but it is normally recommended that you should keep it within the <head> tags.
The <script> tag alerts the browser program to start interpreting all the text between these tags as
a script. A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
Language −This attribute specifies what scripting language you are using. Typically, its
value will be javascript. Although recent versions of HTML (and XHTML, its successor)
have phased out the use of this attribute.
Type −This attribute is what is now recommended to indicate the scripting language in
use and its value should be set to "text/javascript". JavaScript has become default
lannguage in HTML5, and modern browsers, so now adding type is not required.
Hello World!
JavaScript Values
In JavaScript, you can have two types of values.
JavaScript Literals
<html>
<body>
<script>
document.write(10); // Number Literal
document.write("<br />"); // To add line-break
document.write("Hello"); // String Literal
</script>
</body>
</html>
10
Hello
JavaScript Variables
var
let
const
You can use the assignment operator (equal sign) to assign values to the variable.
In the below code, variable a contains the numeric value, and variable b contains the text (string).
<html>
<body>
<script>
let a = 5; // Variable Declaration
document.write(a); // Using variable
document.write("<br>");
let b = "One";
document.write(b);
</script>
</body>
</html>
5
One
<script>
var1 = 10
var2 = 20
</script>
But when formatted in a single line as follows, you must use semicolons −
<script>
var1 = 10; var2 = 20;
</script>
NOTE: It is a good programming practice to use semicolons.
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any
other identifiers must always be typed with a consistent capitalization of letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.
In the code below, we compare the ‘time’ and ‘Time’ strings, which returns false.
<html>
<body>
<script>
let a = "time";
let b = "Time";
document.write("a == b? " + (a == b));
</script>
</body>
</html>
This code will produce the following result –
a == b? false
NOTE: Care should be taken while writing variable and function names in JavaScript.
NOTE: JavaScript doesn’t allow adding the hyphen in variable name or expression name.
JavaScript Keywords
JavaScript contains multiple keywords which we can use for a particular task. For example, the function keyword
can be used to define the function. The let, var, and const keywords can be used to define variables.
Let’s understand the use of the keyword via the example below.
Example
In this example, we used the function keyword to define the function. We used the var keyword inside the function
to define the sum variable.
Also, we used the let and const keywords outside the function to define two variables and initialize them with
values. After that, we called the function using the function name and passed variables as an argument.
<html>
<body>
<script>
function getSum(first, second) {
var sum = first * second;
document.write("The product of " + first + " and " + second + " is " + sum);
}
let first = 3;
const second = 4;
getSum(first, second);
</script>
</body>
</html>
NOTE: JavaScript doesn’t allow to use the of keywords as a variable or expression name.
JavaScript Identifiers
In JavaScript, identifiers are the name of variables, functions, objects, etc.
<script>
pet p = 90;
</script>
The 'test' is an identifier in the below code.
<script>
function test() {
}
</script>
Here are the rules which you should follow to define valid identifiers.
Identifiers should always start with the alphabetical characters (A-Z, a-z), $(dollar sign), or _ (underscore).
It shouldn’t start with digits or hyphens.
The identifier can also contain digits except for the start of it.
Comments in JavaScript
JavaScript supports both C-style and C++-style comments, Thus −
Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.
Any text between the characters /* and */ is treated as a comment. This may span multiple lines.
JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line
comment, just as it does the // comment.
The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->.
Example
<script>
// This is a comment. It is similar to comments in C++
/*
* This is a multi-line comment in JavaScript
* It is very similar to comments in C Programming
*/
</script>
Operators in JavaScript
JavaScript contains various arithmetic, logical, bitwise, etc. operators. We can use any operator
in JavaScript, as shown in the example below.
Example
In this example, we have defined var1 and va2 and initialized them with number values. After
that, we use the ‘*’ operator to get the multiplication result of var1 and var2.
<html>
<body>
<script>
var1 = 10
var2 = 20
var3 = var1 * var2;
var4 = 10 + 20;
document.write(var3, " " ,var4);
</script>
</body>
</html>
200 30
NOTE: When any of two operands of the ‘+’ operator is a string, it converts the other operand to a string and
merges both strings.
Expressions in JavaScript
You can create expressions in JavaScript by combining the variable, values, and operators.
10 + 20;
a * b;
The below expression divides the value of variable c with 2.
c / 2;
Example
In the below code, we have used the assignment and arithmetic expressions.
<html>
<body>
<script>
let a = 10;
let b = 2;
c = 10
d = 12
e=8
The Unicode characters add special characters like emoji, symbols, etc. in the text.
For example, the below Unicode character will display the left arrow.
&larr
The Below Unicode character will display the RS (Rupees sign) symbol.
8360