What is JavaScript?
- JavaScript is the programming language of the web.
- It is a dynamic scripting language that supports prototype-based object construction.
- Many of the basic syntax and language are constructs that are similar to Java and C++.
Getting JavaScript onto your webpage.
- Write it right on the HTML page.
- <script>
- // All JavaScript Code goes here
- </script>
- Import the JavaScript file:
Note: <script> tag will go inside <head> tag on HTML page.
- <script src="sampleJavaScript.js" type="text/javascript"></script>
Before We Get Started,
- It is important to understand how variables work differently in JavaScript.
Scoping of Variables
- Variables in JavaScript are either considered to be local or global.
- Local Variable Scope:- Local variables can only be created in function.
- There is no such thing as block-level scope variables.
Any variables created in any other block of code (code surrounded by curly brackets) will be considered global.
Example: Variables that are part of an if statement are not local to the if statement, it is considered as a global variable.
Global Variable Scope
- var color = "blue";
- if (color)
- {
- var color = "purple"; // this is a global variable,so color will changed to purple.
- console.log(color); // this statement will print purple
- }
- console.log(color); // this statement will print purple
- var color = "blue";
- function printColor()
- {
- var color = "purple"; // this is a local variable
- console.log(color); // this statement will print purple
- }
- printColor();
- console.log(color); // this statement will print blue
- Just like functions in other languages, functions in JavaScript are a block of code used to perform a particular task.
- A function is defined with the function keyword, followed by the name of the function and then a pair of parentheses, which will contain the parameters.
- Functions in JavaScript have other capabilities that they do not have in other object-oriented languages.
Example:
- var x = 3;
- function numSquare(x)
- {
- return x * x;
- }
- var x = 3;
- function numSquare(x)
- {
- return x * x;
- }
- var sentence = "The Square of " + x + " is equal to " + numSquare(x);
- console.log(sentence);
- var num = numSquare(5);
- console.log(num);
- A Special type of function that can be created within JavaScript.
- These functions run automatically. No call to the function needed.
- They can be anonymous or not.
- ((function selfPrint()
- {
- console.log("This function will automatically print this statement);
- })());
- //Be sure to wrap the function in parentheses and add another pair of parentheses at the end of the function.

Nihar NarendraPosted Oct 17, 2016, 12:53 AM
Nc..very good to explain js..
SubashPosted Aug 5, 2016, 4:17 AM
Thank you For Your simple Explanation Sir
Mohammad KhalidPosted Mar 25, 2016, 2:18 AM
Very good article. at one example you have put 5 but you are printing 36. I got confused. Kindly change the result or value. Its a minor typo error.
Amit Kumar SinghPosted Feb 20, 2016, 4:49 AM
Thanku
Sachin BennyPosted Feb 20, 2016, 3:57 AM
nice