Explore essential JavaScript syntax and features, including variables, data types, operators, control flow, functions, and classes. A quick-reference guide for beginners.
Welcome to the JavaScript Syntax and Reference guide, a comprehensive resource designed to help you understand and master the essential syntax and features of JavaScript. This guide is structured to support absolute beginners, providing clear explanations and practical examples. Let’s embark on this journey to explore the building blocks of JavaScript!
Variables are fundamental to programming, allowing us to store and manipulate data. In JavaScript, variables can be declared using var
, let
, or const
.
var
: Declares a variable, optionally initializing it to a value. It has function scope or global scope.var name = "Alice";
console.log(name); // Output: Alice
let
: Declares a block-scoped variable, optionally initializing it to a value.let age = 25;
console.log(age); // Output: 25
const
: Declares a block-scoped, read-only constant.const birthYear = 1995;
console.log(birthYear); // Output: 1995
JavaScript supports several data types, including:
let greeting = "Hello, World!";
let score = 95.5;
true
or false
.let isJavaScriptFun = true;
let undefinedVariable;
console.log(undefinedVariable); // Output: undefined
let emptyValue = null;
let person = {
name: "John",
age: 30
};
let uniqueId = Symbol("id");
2^53 - 1
(introduced in ES2020).let largeNumber = BigInt(123456789012345678901234567890);
Operators are symbols that perform operations on operands. JavaScript includes several types of operators:
+
): Adds two numbers.let sum = 10 + 5; // Output: 15
-
): Subtracts one number from another.let difference = 10 - 5; // Output: 5
*
): Multiplies two numbers.let product = 10 * 5; // Output: 50
/
): Divides one number by another.let quotient = 10 / 5; // Output: 2
%
): Returns the remainder of a division.let remainder = 10 % 3; // Output: 1
**
): Raises the first operand to the power of the second operand.let power = 2 ** 3; // Output: 8
==
): Checks if two values are equal.console.log(5 == "5"); // Output: true
===
): Checks if two values are equal and of the same type.console.log(5 === "5"); // Output: false
!=
): Checks if two values are not equal.console.log(5 != "5"); // Output: false
!==
): Checks if two values are not equal or not of the same type.console.log(5 !== "5"); // Output: true
>
): Checks if the left operand is greater than the right operand.console.log(10 > 5); // Output: true
<
): Checks if the left operand is less than the right operand.console.log(10 < 5); // Output: false
>=
): Checks if the left operand is greater than or equal to the right operand.console.log(10 >= 5); // Output: true
<=
): Checks if the left operand is less than or equal to the right operand.console.log(10 <= 5); // Output: false
&&
): Returns true if both operands are true.console.log(true && false); // Output: false
||
): Returns true if at least one operand is true.console.log(true || false); // Output: true
!
): Inverts the truthiness of the operand.console.log(!true); // Output: false
=
): Assigns a value to a variable.let x = 10;
+=
): Adds a value to a variable and assigns the result to that variable.x += 5; // Equivalent to x = x + 5
-=
): Subtracts a value from a variable and assigns the result to that variable.x -= 5; // Equivalent to x = x - 5
*=
): Multiplies a variable by a value and assigns the result to that variable.x *= 5; // Equivalent to x = x * 5
/=
): Divides a variable by a value and assigns the result to that variable.x /= 5; // Equivalent to x = x / 5
%=
): Applies the modulus operator to a variable and assigns the result to that variable.x %= 5; // Equivalent to x = x % 5
Control flow statements determine the order in which code executes.
if
Statement: Executes a block of code if a specified condition is true.if (x > 0) {
console.log("x is positive");
}
else
Statement: Executes a block of code if the condition in the if
statement is false.if (x > 0) {
console.log("x is positive");
} else {
console.log("x is not positive");
}
else if
Statement: Specifies a new condition if the first condition is false.if (x > 0) {
console.log("x is positive");
} else if (x < 0) {
console.log("x is negative");
} else {
console.log("x is zero");
}
switch
Statement: Evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case.let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Another day");
}
for
Loop: Repeats a block of code a specified number of times.for (let i = 0; i < 5; i++) {
console.log(i);
}
while
Loop: Repeats a block of code as long as a specified condition is true.let i = 0;
while (i < 5) {
console.log(i);
i++;
}
do...while
Loop: Executes a block of code once, and then repeats the loop as long as a specified condition is true.let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
Functions are blocks of code designed to perform a particular task. They can be defined using function declarations, function expressions, or arrow functions.
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
const greet = function(name) {
return "Hello, " + name;
};
console.log(greet("Bob")); // Output: Hello, Bob
Arrow functions provide a more concise syntax for writing function expressions.
const greet = (name) => "Hello, " + name;
console.log(greet("Charlie")); // Output: Hello, Charlie
JavaScript classes, introduced in ES6, provide a more intuitive syntax for creating objects and handling inheritance.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
let dog = new Animal("Dog");
dog.speak(); // Output: Dog makes a noise.
Inheritance allows a class to inherit properties and methods from another class.
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Dog");
dog.speak(); // Output: Dog barks.
Static methods are called on the class itself, not on instances of the class.
class MathUtils {
static add(a, b) {
return a + b;
}
}
console.log(MathUtils.add(5, 3)); // Output: 8
Now that we’ve covered the basics, it’s time to experiment! Try modifying the code examples to see how changes affect the output. For instance, change the values of variables, add new properties to objects, or create additional methods in classes. This hands-on approach will deepen your understanding and boost your confidence in using JavaScript.
To better understand JavaScript’s interaction with web browsers and web pages, let’s visualize some key concepts using diagrams.
graph TD; A[Document] --> B[HTML] B --> C[Head] B --> D[Body] D --> E[Header] D --> F[Main] D --> G[Footer]
Figure 1: The DOM tree structure represents the hierarchical organization of HTML elements in a web page.
graph TD; A[Global Scope] --> B[Function Scope] B --> C[Block Scope]
Figure 2: Variable scope chains illustrate how variables are accessed in different scopes.
For further reading and deeper dives into JavaScript, consider exploring the following resources:
To reinforce your learning, here are some questions and challenges:
var
, let
, and const
?this
keyword in a class method?Remember, this is just the beginning. As you progress, you’ll build more complex and interactive web pages. Keep experimenting, stay curious, and enjoy the journey!
By mastering these fundamental concepts, you’re well on your way to becoming proficient in JavaScript. Keep practicing, and don’t hesitate to revisit this guide whenever you need a refresher. Happy coding!