Start With JavaScript

Now Let's Start With JavaScript
 
We all know about JavaScript:
  • Object-based scripted language
  • A lightweight programming language with less complexity
  • Most commonly used language to add dynamism and interactivity
  • Client and server-side object-based scripted Language
And many more things.
 
Formally JavaScript was known as LiveScript and developed by Netscape and Sun Microsystem. It is also known as ECMAScript since it was standardized by the "European Computer Manufacture's Association".
 
Now let's talk about the key features of JavaScript:
  1. Dynamic Text: This means that the type of variable is defined according to the value stored in it. For example: after declaring a variable x, we can assign it a number or string value. This is known as dynamic typing. JavaScript is an object-based scripting language, that provides some built-in objects, such a String, Math and Data objects.
     
  2. Functional: It implies that JavaScript does not support "classes". Instead of using classes, objects are created from the constructor function. Each constructor function represents a unique object type.
     
  3. Prototype-based: Implies that JavaScript is a prototype-based language. This means that JavaScript uses a prototype instead of classes for inheritance. In JavaScript, each constructor function is associated with a prototype object. There are several built-in objects that represent constructor functions such as: 
    • Array()
    • Boolean()
    • Data()
    • Error()
    • Math()
    • Number()
    • Number()
    • Object()
    • RegExp()
    • String()
       
  4. Imperative and structured: Implies that JavaScript supports all the syntaxes of the structured programming language "C", such as "if" statement, "for" and "switch" statements. The only difference between C and JavaScript is that in JavaScript a semicolon is not necessary to terminate the statement whereas in C the semicolon is necessary to terminate the statement.
     
  5. Platform Independent: Implies that JavaScript supports platform-independency, in other words portability. We can write our JavaScript application and run it on any platform or any browser without affecting the output of the script.
JavaScript in HTML Documents
 
The <SCRIPT> Element allows the use of JavaScript in HTML Documents in one of three ways as in the following:
  1. JavaScript in HEAD Element
  2. JavaScript in BODY Element
  3. As an External Script File
JavaScript in the HEAD Element: We can use JavaScript in the HEAD section by the given method. The script placed inside the HEAD element runs when you perform some action, such as click a link or the submit button.
 
Code Syntax
  1. <HEAD>  
  2. <SCRIPT type="text/javascript">  
  3. Script code here  
  4. </SCRIPT>  
  5. </HEAD> 
Example
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>JavaScript in HEAD Element</title>  
  5.     <h1>  
  6.         Adding script in the HEAD Element</h1>  
  7.     <script type="text/javascript">  
  8. document.write("Welcome to the World of JavaScript  
  9. <BR/>");  
  10. document.write("In this example we have used the  
  11. JavaScript in the HEAD element.");  
  12.     </script>  
  13. </head>  
  14. </html> 
Output
 
JavaScript-1.jpg
 
JavaScript in BODY Element: We can use JavaScript in the BODY section by the given method. The script placed inside the BODY element runs when a web page starts loading in a web browser.
 
Code Syntax
  1. <BODY>  
  2. <SCRIPT type="text/javascript">  
  3. Script code here  
  4. </SCRIPT>  
  5. </BODY> 
Example
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>JavaScript in BODY Elementt</title>  
  5. </head>  
  6. <body>  
  7.     <h1>  
  8.         Adding JavaScript in Body Element</h1>  
  9.     <script type="text/javascript">  
  10. document.write("Welcome to the World of JavaScript  
  11. <BR/>");  
  12. document.write("In this example we have used the  
  13. JavaScript in the BODY element.");  
  14.     </script>  
  15. </body>  
  16. </html> 
Output
 
JavaScript-2.jpg
 
JavaScript as an External Script File
 
When JavaScript code in an HTML file is very lengthy, it affects the readability of the HTML file. In addition, at times we may need to use the same JavaScript code in several Web pages. In such cases, we can store the JavaScript code in an external file and save that file using the ".js" extension. Next we need to link the external file with the HTML document using the "src" attribute of the SCRIPT element to access the script file.
 
Code Syntax
  1. <HEAD>  
  2. <SCRIPT src="URL of the External .js file>  
  3. Script code here  
  4. </SCRIPT>  
  5. </HEAD> 
Example
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>JavaScript as an EXTERNAL Script File</title>  
  5. </head>  
  6. <body>  
  7.     <h2>  
  8.         The sum of First Four Number is:</h2>  
  9.     <h2>  
  10.         <script src="MyScript.js"></H2>  
  11.         </script>  
  12. </body>  
  13. </html>  
  14. External JavaScript : var i, count=0; for(i=0;i<5;i++) { count=count+i; }  
  15. document.write(count); 
Output
 
JavaScript-3.jpg