Skip to main content

Introduction to JavaScript

JavaScript is an object-oriented scripting language used to make web pages. JavaScript can be used only inside a browser. To run JavaScript outside a browser, we need to use Node.js. JavaScript can be used for both client-side and server-side developments.

  • Client-side development: In client side, source code is visible to the user. Client-side is faster compared to server side. It runs on the local computer. HTML, CSS and JavaScript are used for client-side development.

  • Server-side development: In server side, source code is not visible to the user as its output is an HTML page. It runs on the web server. PHP, Python, Java, Ruby are used for server-side development.

<script> Element

The primary method to include JavaScript inside HTML is to use a <script> tag. In general script tag is interpreted as JavaScript, but browsers like internet explorer supports other scripting languages such as VBScript. So, it is better to indicate the scripting language inside script tag. Script tag can be used inside a <head> tag or a <body> tag.

<script language="text/javascript">
alert("Hello world!");
</script>
Note

HTML documents is interpreted line by line, it is better to use <script> tag inside a head tag because head is always read at first. So, it is good idea to put variables and functions inside the head tag.

The above method can be used for a single HTML page containing JavaScript code. If we want to link an external or multiple JavaScript file, we link via the src attribute of <script> tag.

<script type="text/javascript" src="index.js"></script>
or
<script src="danger.js"></script>
Note

Old JavaScript examples use type="text/javascript". The type attribute is not required. JavaScript is the default scripting language in HTML.