JavaScript syntax forms the foundation of writing code in this versatile programming language. It encompasses the rules and structure that define how JavaScript programs are written and interpreted.
JavaScript code consists of statements, which are instructions executed by the browser or JavaScript engine. Each statement typically ends with a semicolon, although it's not always required.
let greeting = "Hello, World!";
console.log(greeting);
Variables store data and are declared using var
, let
, or const
. JavaScript supports various data types, including numbers, strings, booleans, and objects.
let age = 25; // Number
const name = "John"; // String
var isStudent = true; // Boolean
let person = { // Object
firstName: "Jane",
lastName: "Doe"
};
For more details on variables and their usage, check out the guide on JavaScript Variables.
Operators perform operations on variables and values. JavaScript includes arithmetic, comparison, logical, and assignment operators.
let x = 5 + 3; // Addition
let y = 10 > 7; // Comparison
let z = (x > 0) && (y); // Logical AND
To dive deeper into operators, visit our JavaScript Operators guide.
Control structures direct the flow of a program. Common structures include if-else statements, loops, and switch statements.
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
Learn more about conditional statements in our JavaScript If-Else Statements guide.
Functions are reusable blocks of code that perform specific tasks. They can accept parameters and return values.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Outputs: Hello, Alice!
Explore different ways to define functions in our JavaScript Function Declarations guide.
const
for values that won't change, and let
for those that willBy mastering JavaScript syntax, you'll build a solid foundation for creating dynamic and interactive web applications. Remember to practice regularly and explore more advanced concepts as you progress in your JavaScript journey.