What is JavaScript?

JavaScript is an interpreted programming language that appeared in December 1995 (initially called LiveScript). Its script, contained in web documents, can be read and executed by the browser's JavaScript engine whenever the document is loaded into the browser window, allowing for dynamic and interactive content.

How to include JS in HTML

Internal Scripting

You can embed JavaScript directly into your HTML file. Place the script tags in the <head> to run code when a specific user action occurs (like a click), or in the <body> to run the script while the page is initially loading.

<script type="text/javascript" language="JavaScript">
	document.write("Creating a script in an HTML document");
</script>

External Scripting

For cleaner code and better organization, you can link to a separate file (e.g., myscript.js). This is done using the src attribute within the script tag, and the tag itself should remain empty.

<script src="myscript.js"></script>

Variables in JavaScript

JavaScript variables are primarily used to store data. A variable's value can be changed throughout the script, and you reference it by its name to access or modify its stored value.

Syntax:

Variables are declared using the keyword var, followed by the variable's name

var variable-name;

You can declare multiple variables on one line, and initialize them with values:

var countbooks, x=5, y=9;

Key Rules for Naming Variables: