Introduction to JavaScript
JavaScript is the programming language of HTML and the Web. Along with HTML and CSS, JavaScript is one of the three languages used for web development.
JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997. ECMA-262 is the official name of the standard. ECMAScript is the official name of the language.
In HTML, JavaScript programs are executed by the web browser.
Where to Add JavaScript Code
In HTML, JavaScript code must be inserted between <script> and </script> tags. The type attribute is not required. JavaScript is the default scripting language in HTML. You can place any number of scripts in an HTML document. Scripts can be placed in the <body>, or in the <head> section of HTML page, or in the both.
Scripts can also be placed in external files. External scripts are practical when the same code is used in many different web pages. JavaScript files have the file extension .js. To use an external script in HTML, put the source of the script file in the src attribute of a <script> tag.
<script src="/myScript.js"></script>
You can place an external script reference in <head> or <body> as you like. The script will behave as if it was located exactly where the <script> tag is located. Placing scripts in external files has some advantages:
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
JavaScript Functions and Events
A JavaScript function is a block of JavaScript code, that can be executed when "called" for. For example, a function can be called when an event occurs, like when the user clicks a button.
A function is a block of code designed to perform a particular task. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). The parentheses may include parameter names separated by commas. For example,
function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}