Welcome to the JavaScript Basics Tutorial! This guide will help you understand the fundamentals of JavaScript, one of the most popular programming languages for web development.
Table of Contents
Introduction
JavaScript is a high-level, often just-in-time compiled language that conforms to the ECMAScript specification. It is widely used for web development, enabling interactive web pages and dynamic content.
JavaScript vs. Java
Contrary to its name, JavaScript is not related to Java. It was originally inspired by Java but has evolved into a completely different language.
Variables and Data Types
In JavaScript, variables are used to store data values. There are several data types, including:
- Numbers:
var num = 5;
- Strings:
var text = "Hello, World!";
- Boolean:
var isTrue = true;
- Objects:
var person = {name: "John", age: 30};
Control Structures
Control structures allow you to execute code based on certain conditions. Here are some common ones:
- If/Else:
if (condition) { ... } else { ... }
- Switch:
switch (expression) { case value1: ... break; case value2: ... break; default: ... }
- Loops:
for (var i = 0; i < 10; i++) { ... }
andwhile (condition) { ... }
Functions
Functions are blocks of code that can be reused. They can accept parameters and return values. Here's an example:
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("John")); // Output: Hello, John!
Events
JavaScript allows you to add interactivity to web pages by handling events. Common events include:
- Click:
button.addEventListener('click', function() { ... });
- Key Press:
document.addEventListener('keypress', function(event) { ... });
- Mouse Move:
document.addEventListener('mousemove', function(event) { ... });
Further Reading
For more information on JavaScript, we recommend checking out the following resources:
[center]
[center]