JavaScript syntax refers to the rules and conventions that dictate how you write valid JavaScript code. Understanding these rules is crucial for creating functional and readable programs.
Using JavaScript in an HTML file involves including your JavaScript code and utilizing it within your webpage. Here's a detailed explanation of how to use and import JavaScript in an HTML file:
Inline JavaScript:You can embed JavaScript directly into your HTML file using <script> tags. This is often used for small scripts or quick interactions.
<!DOCTYPE html>
<html>
<head>
<title>Inline JavaScript</title>
</head>
<body>
<h1>Inline JavaScript</h1>
<button onclick="alert('Hello, World!')">Click me</button>
<script>
// Your JavaScript code here
function sayHello() {
alert("Hello from inline script!");
}
</script>
</body>
</html>
For larger scripts or when you want to keep your HTML and JavaScript separate, you can use external JavaScript files.
1) Create a JavaScript File:Create a .js file, e.g., script.js, and add your JavaScript code:
// script.js
function sayHello() {
alert("Hello from external script!");
}
In your HTML file, include the external JavaScript file using the <script> tag:
<!DOCTYPE html>
<html>
<head>
<title>External JavaScript</title>
</head>
<body>
<h1>External JavaScript</h1>
<button onclick="sayHello()">Click me</button>
<!-- Include the external JavaScript file -->
<script src="script.js"></script>
</body>
</html>
JavaScript code is structured using semicolons to terminate statements and curly braces to define code blocks.
// Statements are terminated with semicolons
let x = 5;
let y = 10;
let resultText = "x is less than y";
// Code blocks are enclosed in curly braces
if (x < y) {
document.getElementById("demo").innerHTML = resultText;
}
JavaScript is case-sensitive, meaning that variables and function names with different capitalization are treated as distinct.
var myVariable = 42;
var MyVariable = "Hello";
document.getElementById("first").innerHTML = myVariable;
document.getElementById("second").innerHTML = MyVariable;
console.log(myVariable); // Outputs: 42
console.log(MyVariable); // Outputs: Hello
Comments are used to provide explanations and notes within your code.
// This is a single-line comment
/* This is a
multi-line
comment */
JavaScript code is made up of various statements, which perform actions or produce values.
var greeting = "Hello, ";
var name = "Alice";
var message = greeting + name; // Assignment statement
if (name === "Alice") { // Conditional statement
DisplayName(message); // Function call statement
}
function DisplayName(name){
document.getElementById("first").innerHTML = name;
}
Variables are used to store data. They are declared using var, let, or const.
var age = 25; // Old way, less preferred now
let name = "John"; // Variable value can be changed
const PI = 3.14; // Variable value is constant
JavaScript supports several fundamental data types: numbers, strings, booleans, null, and undefined.
let numberVar = 42; // Number
let price = 19.99; // Floating-point number
let stringVar = "Hello"; // String
let booleanTrue = true; // Boolean
let booleanFalse = false; // Boolean
let nullVar = null; // Null
let undefinedVar; // Undefined
let id = Symbol("unique identifier"); // Represents a unique and immutable value often used as object property keys.
JavaScript has reserved words that cannot be used as variable or function names.
var var = "This is a reserved word"; // Syntax error!
var firstName = "John";
var lastName = "Doe";
if (firstName === "John") {
document.getElementById("first").innerHTML = "Hello, " + firstName + " " + lastName;
}
Mastering JavaScript syntax ensures your code is correctly structured and easily understandable.