Windows Programming and Scripting Language

In this section

INTRODUCTION:

JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for enhancing the interaction of a user with the webpage. In other words, you can make your webpage more lively and interactive, with the help of JavaScript. JavaScript is also being used widely in game development and Mobile application development.

intro

The language was initially called LiveScript and was later renamed JavaScript. There are many programmers who think that JavaScript and Java are the same. In fact, JavaScript and Java are very much unrelated. Java is a very complex programming language whereas JavaScript is only a scripting language. The syntax of JavaScript is mostly influenced by the programming language C.

A Simple JavaScript Program

You should place all your JavaScript code within < script > tags (< script > and < /script >) if you are keeping your JavaScript code within the HTML document itself. This helps your browser distinguish your JavaScript code from the rest of the code. As there are other client-side scripting languages (Example: VBScript), it is highly recommended that you specify the scripting language you use. You have to use the type attribute within the < script > tag and set its value to text/javascript like this:

< script type="text/javascript"> < /script >

Hello World Example:

       <html>
            <head>
            <title> My First JavaScript code!!!</title
                <script>
                alert("Hello World!");
                </script>
            </head>
            <body>
        
            </body>
        </html>


output

Language:

  1. Declaring Variables:
  2. Before using a variable, you first need to declare it. You have to use the keyword var to declare a variable. Declaration makes value of the variable “undefined”.

    EX. var name;

  3. Initializing Variable:

    You can assign a value to the variable either while declaring the variable or after declaring the variable.

    EX. name = "John Doe";

    Declaration and initialization may be done in same line.

    EX. var name = "John Doe";

  4. Variable naming rule:

    Smae as other programming languages.

  5. Variable Type:

    • Numbers:

    • Either integers or floating point number.Not required to explicitly declare type.
    • Strings:

    • Text wrapped in either single quotes or double quotes.
      ex. let name = “Your Name”;
    • Boolean:

    • true/false.
      ex. let test = 6 < 3;
    • Arrays:

    • Single object with multiple values.
      ex. let marks = [70,80,87];
      Accessing individual element: marks[0];
    • Object:

    • In JS object are represented as:
      let student = {Name: “Your Name”, RollNo: 42};
      for retrieving information: person.Name;
  6. Conditional statement:

    Decision making statements)
    • if….else statement:

    •            if(conditon){
                          //code;
                      }
                                      else{
                          //code;
                      }
      Can be nested.
    • logical operator:

    • && → And, || → Or, ! →Not
    • Switch…case statement:

    •            switch(expression){
                          case choice1:
                              //code;
                              break;
                          case choice2:
                              //code;
                              break;
                          default:
                              //code;
                      }
  7. Loop:

    • Standard For loop:

    • for(initializer; condition; final-expression){
                              //code;
          }
      --- As with language, break and continue can be used with JS loops.
    • Standard while loop:

    • initializer;
                      while(condition){
                      //code;
          }
    • do…while loop:

    • initializer;
                      do{
                      //code;
          }while(condition);
  8. Function:

    Set of statements that performs a task/computation.
    • Declaration:

    • starts with keyword function followed by name of the function, list of coma separated parameters and statements inside curly braces. ex. function print_hello(name){ console.log(“Hello ” +name); } Primitive parameters such as number are passed to function by value. Changes inside the function is not reflected globally. Non-primitive value like Array or user-defined object when passed as parameter, changes in the properties done inside the function are visible globally.
    • Function Expression:

    • Can assign function to a variable anonymously or by giving it a name.
      ex. const square = function(num){
               return num ** 2;
           } → square(4);
           const factorial = function fac(num){
               return num < 2 ? 1 : num * fac(num – 1);
           } → console.log(factorial(3))

Conclusion: