JavaScript Introduction
- JavaScript is a loosely-typed client side scripting language that executes in the user's web browser.
- It is a lightweight, interpreted programming language with object-oriented capabilities.
- Designed for creating network-centric applications.
JavaScript syntax
- JavaScript code can be written inside <script> tag.
- External JavaScript file (.js) can be referenced using <script src="/PathToScriptFile.js"></script> where src attribute is used to specify the full path of .js file.
- JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.
- We can place the <script> tag anywhere within the html web page, but it is normally recommended that it should be kept within the <head> tag and end the body tag.
JavaScript is a case sensitive language. This means that the language keywords, variables, function names, and any others identifiers must always be typed with a consistent capitalization of letters.
For example: online, Online, OnLine, and ONLINE are four distinct variable names.
JavaScript is a Dynamic Language
JavaScript is dynamically typed, that means types of variables are generally not known at compile time.
JavaScript Datatypes are Inferred
Type inference means you don't have to worry about declaring types everywhere.it automatically choose preferred datatype according to what is being assigned to variable.
In the above example, the 'x' variable contains string value as well as number value and displays both values one after another without any error.
Var for Declaration
The var keyword is used to create new variables in the current scope. var respects the scope ,without var variables are global.
In above, variable 'x' is not recognized outside the scope because 'x' is local variable. so, it encountered the error as below:
If we had not defined 'x' variable using var, then 'x' would become the global variable which could be accessed from everywhere.
JavaScript Hoisting
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.
for example:
No comments:
Post a Comment