JavaScript Comments, Identifiers, Keywords, Statements, Expression

In this tutorial, you will learn javascript syntax with the following:

  • JS Case-Sensitive
  • Identifiers
  • Comments
  • Statements
  • Expressions
  • Keywords

JS Case-Sensitive

In JavaScript each thing like variables or identifiersfunction names, class names and operators are case-sensitive. This means that vol and Vol are two different variables in js.

For e.g. you cannot use  catch for the function name, because it is a reserved keyword in js. However, Catch is a valid function name.

Example 1: case-sensitive in js

<script>
function Catch() {
   document.write("Hello, My Coding Ground!");
}
Catch();
</script>

Identifiers

As the name suggests, An identifier is the name of a variablefunction, parameter, and class in js.

Rules of writing an identifier:

  • The first character must be a letter (a-z, or A-Z), an underscore(_), or a dollar sign ($).
  • The other characters can be letters (a-z, A-Z), numbers (0-9), underscores (_), and dollar signs ($).

Please keep in mind that when we talk about “letters” in this context, we’re not just referring to the basic letters you find on a standard keyboard. We’re also talking about extended characters that can be found in ASCII or Unicode. However, it’s generally not recommended to use these extended characters.

When you’re writing identifiers in JavaScript, it’s best to use camel case. Camel case means that you write the first word in lowercase, and then capitalize the first letter of each additional word. This is a great practice to follow!

Example 1: Valid Identifiers js

counter
inArray
beginWith
redirectPage

Comments

In any programming languages, comments are very good practice. Because it make readable your codes.

JavaScript supports both single-line and block comments.

A single-line comment starts with two forward-slash characters (//).

Example 1: single line comment in js

// this is a single-line comment

When you want to write a block comment in your code, you can start it off with a forward slash and asterisk (/) and then end it with the opposite (/).

Example 2: Multi line comment in js

/*
* This is a block comment that can
* span multiple lines
*/

It is a good practice to use an asterisk to begin the comment line for readability purposes.

Statements

When you’re writing statements in JavaScript, you can use a semicolon to signal the end of a statement. This is important because it helps make your code more readable and can help you avoid problems that might come up otherwise. So don’t forget to use semicolons when you’re writing your code!

Example 1: JS Statement

See the following example for statement:

var a = 10;
var b = 20;

To group together multiple statements in JavaScript, you can use a code block that starts with a left curly brace ({) and ends with a right curly brace (}). This is a handy way to keep related statements together and can make your code easier to read and understand.

if( a > b) {
   console.log('a is greater than b');
   return 1;
}

Expressions

In js, an expression is a piece of code that evaluates to a value.

Example 1: Expression in js

	
a = 2 + 5;

The above expression returns 7 so it is a valid expression.

Suppose you have two variables a and b and want to add both using the + operator.

See the following example:

a = 5;
b = 10;
c = a + b;

Keywords

Like many programming languages, JavaScript has certain reserved keywords that have specific purposes. When you’re writing code in JavaScript, you’ll want to make sure you’re using these keywords correctly and not using them as identifiers.

Each of these keywords has its own unique purpose in JavaScript. Here’s a list of some of the most important ones to keep in mind:

breakexportsuper
caseextendsswitch
catchfinallytypeof
classforthis
constfunctionthrow
continueiftry
debuggerimportvar
defaultinvoid
deleteinstanceofwhile
donewwith
elsereturnyield

Conclusion

In this tutorial, you have learned JavaScript syntax including naming an identifier, constructing statements, and making comments in code.

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *