Saturday, August 28, 2021

Brief Introduction on JavaScript

Introduction

  1. JavaScript was first appeared in Dec 4, 1995 has become the most popular programming language at the current time.
  2. It is a phenomenal programming language for the web page.
  3. Moreover, it is abbreviated as JS, is dynamic in discipline that assist webpage developing.
  4. Further, it is an interpreted language whose paradigm is imperative to statement as FIFO phenomenon.


1. Script Tag

  1. To write JS code inside the html page, requires a special tag, and that tag is called script tag. It is opened by <script> and closed by </script>.
  2. The JS code is written inside the script tag.
  3. The script tag can be defined either in the head tag or in the body tag.
  4. One thing we must have to consider is script tag defined in the head tag may not be able to access the DOM(Document Object Model) elements because the head tag loads before body tag.
  5. Therefore, in order to access DOM elements, script tag must be defined inside body tag.
      The picture below shows how to use script tag.


 








2. Case sensitive JavaScript

  1. JS is case sensitive language, it means that language keywords, variables, function names, and any other identifiers must be typed with a consistent capitalization of letters.
  2. For example; while keyword must be typed "while" not "While" or "WHILE". Also, food and FOOD are different variables.
We can make out vision clear from the following picture.


3. JavaScript is Dynamically typed not Statically

  1. JavaScript is dynamically typed language, this implies that the type of the variable is checked during run time only.
  2. Therefore, JavaScript is not statically typed because statically typed language checks the variable type at compile time.
  3. Hence, it is stated as loosely typed language.
we can visualize figure below to make ourselves clear.




















4. Datatype inference in JS

    1. Datatype of JS are inferred, this implies that there is no need to babysit for the compiler in order to define the datatypes everywhere.
    2. Further, datatype inference means, just define the value to the variable that we want, and its datatype will be declared according to the nature of value  defined.
    3. Therefore, merely we can say JavaScript is autonomous in nature, means it acts freely.
we can look up below for more clarification.























From above figure, we can see that a variable "name" is declared whose datatype is unknown. But after the value="Prashant" of the variable is defined, its datatype will be known to "string". This is datatype inference. JavaScript is inference in nature in such a way that it does not need to define the datatype to the variables declared. Automatically, according to the nature of variable, datatype is set.

5. Role of var in JS.

  1. The var keyword is used to create a new variable in the current context or scope being used.
  2. Var keyword declares both local and global variables. It depends upon the way it is declared. If the var keyword is used to declare variable outside the function or block then it is declared as global variable. 


From above picture we can see that, a variable name  is declared outside the function, which is defined as name="Prashant". Once is alert inside the function Person() and next time it is again alert outside the function. 
Then, output will be two times alert of name because, global variable is used anywhere throughout the script. We can see the output visualization as.





When the variable is declared inside the function or block then at that time, the variable shows behavior as local variable. This means that the variable can be accessed only inside the block or that function.


6. JavaScript Hoisting

JavaScript Hoisting means that the JavaScript engine moves all the declarations to the top of the script. For example, we can use the variable and then we can declare the variable. This is a kind of flexibility of JavaScript.


From above picture we can see that, the variable is used first without declaration and after that only variable is being declared. That is, Prashant is assigned to name which is not declared yet. And, right after the name being alert, variable name is declared.




 


2 comments:

IIFE(Immediately Invoked Function Expression) & Arrow function

 Immediately Invoked Function Expression(IIFE) is an anonymous function which gets invoked immediately. It is anonymous because it does not ...