JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print How to Print a Page Using JavaScript Textbox events in JavaScript How to find the largest number contained in a JavaScript Array?

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions JavaScript Beautifier Practice Javascript Online Object in JavaScript JavaScript Count HTML Interpreter Getters and Setters in JavaScript Throw New Error in JavaScript XOR in JavaScript Callbacks and Promises in JavaScript Atob in JavaScript Binary in JavaScript Palindrome Number in JavaScript How to Get First Character Of A String In JavaScript How to Get Image Data in JavaScript How to get URL in JavaScript JavaScript GroupBy Methods difference-between-var-let-and-const-keyword-in-java-script JavaScript Beautifier Iterating over Objects in Javascript Find the XOR of two numbers without using the XOR operator

Javascript Example

<!DOCTYPE html>  
<html>  
<body>  
<h2>Welcome to JavaScript</h2>  
<script>  
document.write("Hello Friends");  
</script>  
</body>  
</html>
Try Now JavaScript can be implemented using JavaScript statements that are placed within the <script>...</script>.
<!DOCTYPE html>  
<html>  
<body>  
<script type= "text/javascript">  
document.write("Hello World");  
</script>  
</body>  
</html>
Try Now

JavaScript code can be written in 3 places in JavaScript

  1. Between the <body>......</body> tag of HTML.
  2. Between the <head>......</head> tag of HTML.
  3. In .jsfile(external JavaScript).

Example (code between the body tags)

The below example displays JavaScript code in body tag.
<!DOCTYPE html>  
<html>  
<body>  
<script type="text/javascript">  
alert("Hello world");  
</script>  
</body>  
</html>
Try Now

Example2 (code between the head tag)

In below example we are creating function msg(). To create a function in JavaScript need to write function in function_name. To call function we need to work on event. Here, we are using one click event to call msg() function.
<!DOCTYPE html>  
<html>  
<head>  
<script type="text/javascript">  
function msg(){  
alert("Hello world");  
}  
</script>  
</head>  
<body>  
<p>Welcome to Javascript</p>  
<form>  
<input type="button" value="click" onclick="msg()"/>  
</form>  
</body>  
</html>
Try Now

External JavaScript file

We can create external JavaScript file and include it into HTML files. It provides code reusability because single JavaScript file can be used in several HTML pages. It increases the speed of the web page. File must be saved in .js extension.

Example

<!DOCTYPE html>  
<html>  
<head>  
<script type="text/javascript" src="message.js"></script>  
</head>  
<body>  
<p>Hello world</p>  
<form>  
<input type="button" value="click" onclick="msg()"/>  
</form>  
</body>  
</html>
Try Now