In JavaScript, variables are used to store data. You can declare variables using var
, let
, or const
.
// Using var
var age = 25;
// Using let
let name = "John";
// Using const (constant value)
const PI = 3.14;
var
has function - scope, while let
and const
have block - scope. const
cannot be reassigned after initialization.
JavaScript has several data types:
let num = 10;
let floatNum = 3.14;
let message = "Hello, World!";
true
or false
.let isAdult = true;
let emptyValue = null;
let notSet;
console.log(notSet); // Output: undefined
let person = {
name: "Alice",
age: 30
};
Functions are reusable blocks of code. You can define functions in several ways.
// Function declaration
function add(a, b) {
return a + b;
}
// Function expression
let multiply = function(a, b) {
return a * b;
};
// Arrow function
let divide = (a, b) => a / b;
console.log(add(2, 3)); // Output: 5
console.log(multiply(4, 5)); // Output: 20
console.log(divide(10, 2)); // Output: 5
let score = 70;
if (score >= 60) {
console.log("Passed");
} else {
console.log("Failed");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
let j = 0;
while (j < 3) {
console.log(j);
j++;
}
You can embed JavaScript code directly in an HTML file using the <script>
tag.
<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<script>
alert("Hello, World!");
</script>
</body>
</html>
It is a good practice to separate JavaScript code into external files.
index.html
<!DOCTYPE html>
<html>
<body>
<h1>External JavaScript</h1>
<script src="script.js"></script>
</body>
</html>
script.js
console.log("This is an external JavaScript file.");
The Document Object Model (DOM) is a programming interface for HTML and XML documents. You can use JavaScript to manipulate the DOM.
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a paragraph.</p>
<script>
let para = document.getElementById("demo");
para.innerHTML = "New text in the paragraph.";
</script>
</body>
</html>
Events are actions that occur in the browser, such as a user clicking a button. You can use JavaScript to handle these events.
<!DOCTYPE html>
<html>
<body>
<button id="myButton">Click me</button>
<script>
let button = document.getElementById("myButton");
button.addEventListener("click", function() {
alert("Button clicked!");
});
</script>
</body>
</html>
let a = 10;
, use let numberOfStudents = 10;
.// This function calculates the area of a circle
function calculateCircleArea(radius) {
return Math.PI * radius * radius;
}
Use try...catch
blocks to handle errors gracefully.
try {
let result = 10 / 0; // This will throw an error
} catch (error) {
console.log("An error occurred: " + error.message);
}
Break your code into smaller, reusable modules. In modern JavaScript, you can use the import
and export
statements.
math.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
main.js
import { add, subtract } from './math.js';
console.log(add(2, 3));
console.log(subtract(5, 2));
JavaScript is a powerful and versatile programming language. By understanding the fundamental concepts, usage methods, common practices, and best practices outlined in this blog, beginners can start their journey from zero knowledge to becoming proficient in JavaScript. With continuous learning and practice, you can build complex web applications, server - side programs, and more.