JavaScript Basics
JavaScript is a versatile programming language that powers the web. Let's start with the fundamentals.
Variables and Data Types
JavaScript has several data types:
javascript
// Numbers
let age = 25;
const pi = 3.14159;
// Strings
let name = "John Doe";
const message = 'Hello World';
// Booleans
let isActive = true;
const isComplete = false;
// Arrays
let fruits = ["apple", "banana", "orange"];
const numbers = [1, 2, 3, 4, 5];
// Objects
let person = {
name: "John",
age: 30,
city: "New York"
};Functions
Functions are reusable blocks of code:
javascript
// Function declaration
function greet(name) {
return `Hello, ${name}!`;
}
// Arrow function
const add = (a, b) => a + b;
// Function with default parameters
function createUser(name, age = 18) {
return { name, age };
}Key Concepts
- Hoisting: Variable and function declarations are moved to the top
- Closures: Functions have access to variables in their outer scope
- Prototypes: JavaScript's inheritance mechanism
- Asynchronous Programming: Promises, async/await, and callbacks
Practice Exercises
- Create a function that calculates the area of a circle
- Write a function that reverses a string
- Implement a simple calculator with basic operations
Next Steps
Once you master these basics, you can move on to:
- DOM manipulation
- Event handling
- Asynchronous JavaScript
- Modern ES6+ features