JavaScript Syntax
A Quick Overview of JavaScript Syntax
JavaScript is a powerful programming language used to create interactive web pages. Whether you’re building forms, animations, or games, JavaScript syntax plays a crucial role in making things work. Let's quickly look at the basic syntax to get started.
1. Variables & Constants
In JavaScript, we use let
and const
to declare variables and constants. let
is used for variables that can change, and const
is used for values that should remain constant.
let name = 'Alice'; // Variable
const age = 25; // Constant
2. Data Types
JavaScript has different data types such as String
, Number
, Boolean
, Object
, and Array
to store and manipulate data.
let name = 'Alice'; // String
let age = 25; // Number
let isStudent = true; // Boolean
let person = { name: 'Alice', age: 25 }; // Object
3. Functions
Functions are reusable blocks of code that perform specific tasks. You can define them using the function
keyword or use the modern arrow function
syntax.
// Function Declaration
function greet(name) {
console.log('Hello, ' + name);
}
// Arrow Function
const greet = (name) => console.log('Hello, ' + name);
4. Control Structures
JavaScript uses if
, else
, and switch
statements to make decisions based on conditions. It also uses loops like for
and while
to repeat code.
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}
// Loop Example
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}
This was a quick overview of some basic JavaScript syntax. JavaScript is a flexible and powerful language that you can use to add interactivity and dynamic content to your web pages. By understanding these fundamental concepts, you're on your way to building more engaging and functional websites!