JavaScript Tutorial – Introduction

Learn With Examples

Each chapter of this tutorial has examples that you can try yourself to better understand the topic. You can practice the JavaScript examples with our online JavaScript editor.

Where to place JavaScript Code

Unlike HTML, you have to let browsers know in advance if you want to use JavaScript. JavaScript can be placed in the <body> and the <head> sections of a HTML page. When placing the JavaScript in the HTML, it must be inserted between <script> and </script> tags.

Hello World JavaScript Example

		
<!DOCTYPE html>
<html>
	<head>
	</head>
	<body>
		<script type="text/javascript">
			document.write("<h1>Hello World!</h1>");
		</script>
	</body>
</html>
		
	

External JavaScript

JavaScript can also be placed in external file with .js as the file extension. There are a few advantage of using external JavaScript, such as separating HTML and code and allow browsers to cache JavaScript files to speed up page load time. We will go into details about external JavaScript later.

How to Link External JavaScript

		
<!DOCTYPE html>
<html>
	<head>
		<script src="/whatAboutHTML.js"></script>
	</head>
	<body>
	</body>
</html>