Monday, August 24, 2020

JavaScript Syntax

Whitespace and new lines

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript code. You are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.

Semicolons

S tatements in JavaScript generally end with a semicolon character, similar to C, C++, and Java. JavaScript, however, allows you to omit this semicolon if each of your statements are placed on a separate line.

  • Example 1

  •       varA  = "Learn"
  •       varB = "JAScript"

  • Example 2

  •       varA  = "Learn" varB = "JAScript"

  • Example 3

  •       varA  = "Learn"; varB = "JAScript";

In the above examples Example 1 and 3 will run while example 2 will generate error.

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 in consistency.

  • Example 4

  •     var myVar = 5;
  •     var myvar = 10;
  •     print(myVar);
  •     print(myvar);

        Example 5

       function myFunction(){
         return 5;
  • };

  •  function myfunction(){
        return 10;
  • };

In the above example myVar and myvar and two different variables and they will print 5 and 10 respectively. In example 5, the two functions are totally different


Comments in JavaScript

JavaScript supports both C-style and C++-style comments.

    1. Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.
    2. Any text between the characters /* and */ is treated as a comment. This may span multiple lines.

  • Example 6

  • //This is a single line comment
  • var val = 5;
  • var name = "Tree";
  • var combined = val+name;
  • print(combined);
  • /*
  • This
  • is
  • a
  • multiline
  • comment
  • */

No comments:

Post a Comment

JavaScript Syntax

Whitespace and new lines JavaScript ignores spaces, tabs, and newlines that appear in JavaScript code. You are free to fo...