JavaScript is a client side language that makes our website interactive. The JavaScript Code is known as script.
Table of Contents
JavaScript Introduction
JavaScript is a programming language which runs on browser and web browsers have JavaScript Engine to run it. JavaScript is a scripting language and is also known as user-side language.
JavaScript also abbreviated as JS, is an interpreted programming language. JavaScript is comparatively a very lightweight and just-in-time compiled programming language with pre-defined functions.
JavaScript follows ECMAScript specification.
JavaScript History
JavaScript was invented by Brendan Eich in 1995. It was initially called as LiveScript. Unlike HTML and CSS, JavaScript is a programming language. It gives Life to our web pages, due to JavaScript we can interact with our web pages like never before. JavaScript is an object oriented programming language.
JavaScript is a Scripting Language
JavaScript is also known as Scripting Language. Scripting Language is a language that doesn't need compilation and works in run time environment.
JavaScript is a Client Side Language
JavaScript is a Client Side language because it runs on browser due to which it has a great capability to interact with the user. But nowadays JavaScript is also used at the server side.
Note: JavaScript and Java are completely different languages.
Why We Use JavaScript
JavaScript is very powerful language and it can perform tasks that HTML and CSS cannot because they are not programming languages. Some of the tasks that JavaScript can perform are:
- Modify HTML or CSS of our web page without reloading it.
- Listen to events like mouse click, mouse movement, key presses etc.
- Move things on the screen.
- Send requests to remote servers.
- Form Data validation before it get's further processed.
- Communicate data between server and browser.
- Interact with devices like microphone, web cams etc.
- Hide and Show content of web page.
- Cool Animations.
- Make Awesome Web Games and much more.
Where to write JavaScript
Just Like CSS we can include our JavaScript into our HTML file using two ways.
- Internal or In-Page JavaScript Code
- External JavaScript Code
Internal or In-Page JavaScript Code
Internal JavaScript code is written within the HTML file. It must be put inside the script opening and closing tags. Look at the code given below.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
/* JavaScript Code Here */
</script>
</body>
</html>
As you might have noticed, I have put the script tag at the end of the body of my page. It's because JavaScript takes time to load and if i put my script tag before the content of the page then it won't be displayed until my JavaScript is completely loaded.
So it's the best practice to write your JavaScript at the end, just before the closing body tag.
I can also write my JavaScript code in head section of my web page. Look at the code below.
<!DOCTYPE html>
<html>
<head>
<script>
/* JavaScript Code Here */
</script>
</head>
<body>
</body>
</html>
But this will also cause same problem, because the browser will load the script first and content of main page won't be displayed until all the script is loaded.
So again, always put your script tag just before the closing body tag.
External JavaScript Code
Just like CSS, you can also write your JavaScript code in another file and then include it in your main HTML file or web page.
For that you have to save your JavaScript file with .js extension and then include it using source attribute (src) in script tag. Look at code given below.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="myJavaScriptCode.js" >
</script>
</body>
</html>
Advantages of External JavaScript Code
When JavaScript code is loaded from an external file, your browser doesn't have to load the script again and again and the cached JavaScript code makes the web browsing faster. Also it's easier to read the code when everything (HTML, CSS and JavaScript) isn't inside a single file.