JavaScript Syntax

In this tutorial, you will learn some Characteristics of  JavaScript Syntax.

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program. Let us Explore more detail about the properties of JavaScript which will help us to write the code more efficiently.

JavaScript contain statements that are placed within the <script></script>  tags of the HTML document  on the web page, or within the external JavaScript file having .js extension.

Let us see the Basic Example

<script> 
document.write("Basi JavaScript code"); 
</script> 

do not worry if you did not understand statements and functions in this article, we will learn every topic step by step in further tutorials.

JavaScript Character Set

Unicode is made up of ASCII and Latin-1 that supports every written language currently used on the programming world and JavaScript uses Unicode character set and that allows almost all characters and symbols.

<script src="codedec.js" charset="utf-16">

JavaScript Case sensitivity

JavaScript is a case-sensitive language. including variables, function names, class names, language keywords and operators are case-sensitive. It must always be typed with a consistent capitalization of letters.

For Example the method name getElementByid() must be typed with the exact case not as getElementByID().

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript is Case Sensitive Language</h1>

<p id="sample"></p>

<script>
var lastName = "CODEDEC";
var lastname = "CODEBUN";
document.getElementById("sample").innerHTML = lastName;
</script>

</body>
</html>

OUTPUT:

 

  Syntax Note
** Likewise, variable var cannot be taken as the name of a function because it is a keyword. However, Var can be a valid function name. JavaScript uses Camel case like FirstName, LastName etc.**

JavaScript String

JavaScript String is an object that represents a sequence of characters text that must be enclosed single or double quotation marks.

For Example:

<script>
    
    "Hello codedec" //double quotation
    
    'Hello codebun' //single quotation 

</script>

JavaScript Number & boolean

JavaScript allows every value of the numbers writing as a code but the numbers should not be under quotation marks, we can use values of number in integer, float, hexadecimal etc.
Example:

<script>
var x = 235; // interger value
var y = 6.25; // float value
var a = x + y;
document.getElementById("demo").innerHTML = a;
</script>

The boolean value of true and false is also included in the JavaScript programming language.
Example:

<script>
function mycodedecfunction() {
  document.getElementById("sample").innerHTML = Boolean(20 > 15);
}
</script>

JavaScript Identifiers

An Identifier is simply a name, In JavaScript Identifiers are the name given to labels like variable, functions etc. It is used the same as in other programming languages like Java, C++ etc.

Let us discuss the formal format of Identifier in JavaScript

  • The identifier consists of one or more character. The first character must be a letter (a-z, or A-Z), an underscore (_), or a dollar sign ($).
  • JavaScript reserved keywords are not used as a variable name. All succeeding Character can be letters (a-z, A-Z), numbers (0-9), underscores (_), and dollar signs ($).
  • Numbers are not allowed as the First Character and JavaScript variable names are case-sensitive as such Var and VAR are two different variables.

Example:

JavaScript Variable

Variables are the names of storage location where the data values of programs are stored. the equal sign is used to assign the value of the variable. There are two types of Variable used in JavaScript :

1. Local variables.
2. Global variables.
Example:

<!DOCTYPE html>
<html>
<body>

<h3> Example of JavaScript Variables</h3>
<p id="sample"></p>

<script>
var z; // variable declared 
x = 10;  // variable value assigned.
document.getElementById("sample").innerHTML = 10;
</script>

</body>
</html>

OUTPUT:

JavaScript Operator

Operators are the symbols in JavaScript that are used to perform the operations on operands like using assignment operator (=) in JavaScript to assign the value to variables.

Example:

<!DOCTYPE html>
<html>
<body>

<h3>Example of Operators in JavaScript.</h3>

<p id="sample"></p>

<script>
var x = 35; // variable value defined using assignment operator

var y = 25; // variable value defined using assignment operator

document.getElementById("sample").innerHTML = x + y; // variable value defined using arthimatic (+) operator.
</script>

</body>
</html>

OUTPUT:

JavaScript Keywords

Keywords are reserved words for a special use in JavaScript, which is used to identify the action of the program and it cannot be used as variable names or function names.

Example:

<!DOCTYPE html>
<html>
<body>

<h3>Example of keywords in JavaScript</h3>

<p id="sample"></p>

<script>
var a = 5 * 5; // var keyword is used to define the value of identifier.
var b = a + 10;
document.getElementById("sample").innerHTML = b;
</script>

</body>
</html>

OUTPUT:

JavaScript Comments

Comments are simply a line of text that is completely ignored by the JavaScript, It is used for better understanding of code by enhancing the readability of the JavaScript code and it is written after double slashes //.

Example:

// This is my first JavaScript program
document.write("Hi Codedec!");