![](https://static.wixstatic.com/media/0f65e1_fa7adcf679304644bd174bea340b6002~mv2.png/v1/fill/w_595,h_321,al_c,q_85,enc_auto/0f65e1_fa7adcf679304644bd174bea340b6002~mv2.png)
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document. However, if your script needs to run at a certain point within a page’s layout — like when using document.write to generate content — you should put it at the point where it should be called, usually within the <body> section.
Code Example 1:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Today's Date</title>
<script>let d = new Date();
alert("Today's date is " + d);
</script>
</head>
<body>
</body>
</html>
Output:
![](https://static.wixstatic.com/media/0f65e1_7b6eaf32a1fe4eff9c6da2badb4a9c80~mv2.png/v1/fill/w_980,h_292,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/0f65e1_7b6eaf32a1fe4eff9c6da2badb4a9c80~mv2.png)
Code Example 2:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Today's Date</title>
</head>
<body>
<script>let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
</script>
</body>
</html>
Output:
![](https://static.wixstatic.com/media/0f65e1_101a226b6f044febaeeb77fcd6f8341c~mv2.png/v1/fill/w_790,h_363,al_c,q_85,enc_auto/0f65e1_101a226b6f044febaeeb77fcd6f8341c~mv2.png)
Advantages of JavaScript
JS is faster
Simple to use
Online Support
Inter-operability
Modern-day Scripting Language
Disadvantages of JavaScript
Client-Side Security
Code Visibility
Time consuming
Advanced Competitor
Disabling JavaScript can hinder a web page
The Tech Platform
Comments