Javascript Syntax By albro

Words
744
Reading
4 min
Listen
Play
3y

Javascript Syntax

Have you ever come across the word Syntax? This word is a very important word; Both in terms of the meaning of the word itself and in terms of its practical meaning. According to the Oxford dictionary, the general meaning of this word is as follows:

the rules that state how words and phrases must be used in a computer language

(w3schools) JavaScript syntax is the set of rules, how JavaScript programs are constructed.

In other words, in human languages we have "grammar" and in programming languages we have syntax!

Values in JavaScript

JavaScript defines two types of values:

  • constant values (literals)
  • variable values
  • We will discuss both types of these values.

    Constant values in JavaScript

    The most important rules for writing constant values are the following:

    Numbers can be written with or without decimals. Example:

    
    
    
    JavaScript Numbers
    Number can be written with or without decimals.

    document.getElementById("demo").innerHTML = 10.50;

    Number Constants

    Strings are treated as text and can be enclosed between double quotations or single quotations. Example:

    "Hive Blockcahin JS Bot with @albro"
    'Hive Blockcahin JS Bot with @albro'
    
    
    
    JavaScript Strings
    String can be enclosed between double quotations or single quotations.

    document.getElementById("double").innerHTML = "Double Quotation: Hive Blockcahin JS Bot with @albro"; document.getElementById("single").innerHTML = 'Single Quotation: Hive Blockcahin JS Bot with @albro';

    String Constant

    Variable values in JavaScript

    Variable values are stored in variables. Variables are used in programming languages to store data. JavaScript uses the word var to define variables and the equal sign (=) to assign values to variables. In the following example, X is defined as a variable and then the value 6 is assigned to it:

    
    
    
    JavaScript Variables
    In this example, x is defined as a variable. Then, x is assigned the value of 6:

    var x; x = 6; document.getElementById("demo").innerHTML = x;

    JavaScript Variables

    Operators in JavaScript

    JavaScript uses arithmetic operators to calculate values(+ - * /). Example:

    
    
    
    JavaScript Operators
    JavaScript uses arithmetic operators to compute values (just like algebra).

    document.getElementById("demo").innerHTML = (5 + 6) * 10;

    The output of this code will be 110

    The assignment operator (=) is used to assign values to variables. Example:

    
    
    
    Assigning JavaScript Values
    In JavaScript the = operator is used to assign values to variables.

    var x, y; x = 5; y = 6; document.getElementById("demo").innerHTML = x + y;

    Assigning JavaScript

    Expressions in JavaScript

    An expression is a combination of values, variables and operators that leads to a value. Calculating this expression is called evaluation. Simple example:

    5 * 10

    These expressions can also include variables:

    
    
    
    JavaScript Expressions
    Expressions compute to values.

    var x; x = 5; document.getElementById("demo").innerHTML = x * 10;

    JavaScript Expression

    These expressions can also contain string values:

    
    
    
    JavaScript Expressions
    Expressions compute to values.

    document.getElementById("demo").innerHTML = "Hive" + " " + "Blockchain";

    The output of this code will be "Hive Blockchain".

    Comments in JavaScript

    Not all statements can be executed in JavaScript. Comments are from this category of statements. You can write comments in two ways:

    • Single line comment: The text of the comment is placed after //.
    • Multi-line comment: Comment text is placed between /* and */.

    Note: The difference between a single-line comment and a multi-line comment is that if you use a single-line comment, after pressing the enter key or exiting that line in any way, what you write will not be considered as a comment. Example:

    
    
    
    JavaScript Comments are NOT Executed

    var x; x = 5;
    // x = 6; Not Execute
    /* This code Not Execute */
    document.getElementById("demo").innerHTML = x;

    Identifiers in JavaScript

    Identifiers are used in JavaScript to name variables, keywords, functions and labels. Naming rules are the same in most programming languages. In JavaScript, the first letter of Identifiers (the same variable name, or function name, etc.) must be a letter, or an underscore (_) or a dollar sign ($). The letters after the first letter can be letters, numbers, underscores or dollar signs. Therefore, numbers are not acceptable as first letters.

    Important point: Variables in JavaScript are case sensitive; That is, they are sensitive to small and large English letters. For example, a variable called Platform is not the same as a variable called platform!

    
    
    
    JavaScript is Case Sensitive
    Try change lastName to lastname.

    var lastname, lastName; Platform = "Platform is Uppercase"; platform = "platform is Lowercase"; document.getElementById("demo").innerHTML = Platform;

    The output of this code will be the string "Platform is Uppercase".

    So be sure to pay attention: based on what was said, you should know that JavaScript does not recognize VAR or Var as the var keyword (to define a variable)!

Javascript Syntax By albro | Ecency