JS Statements – Basics
JavaScript is a versatile programming language widely used for web development, but it’s often perceived as complex. At its core, JavaScript is a scripting language that allows you to make web pages interactive. It’s a fundamental part of any web development workflow, and understanding the basics is crucial for anyone starting out. This tutorial will cover the fundamental concepts of JavaScript statements, providing a solid foundation for your learning journey.
💻 Introduction to JavaScript Statements
JavaScript statements are the building blocks of any JavaScript code. They are instructions that tell the JavaScript engine what to do. These statements are executed in a sequential order, and the engine processes them one at a time. Think of them as commands that your browser or web application understands. The most basic type of statement is the var, let, and const declaration.
📝 Declaring Variables
Variables are used to store data in your program. In JavaScript, you declare variables using the var, let, or const keyword.
-
var: Historically,varwas the primary way to declare variables. However, it's generally considered best practice to avoid usingvarin modern JavaScript due to its function scope issues.var name = "Alice"; console.log(name); // Output: Alice -
let:letis used to declare variables that can be reassigned. It's generally preferred overvarbecause it has block scope, meaning the variable is only accessible within the block of code where it's defined.let age = 30; console.log(age); // Output: 30 age = 31; // This is allowed because we're reassessing the variable console.log(age); // Output: 31 -
const:constis used to declare variables that should not be reassigned after they've been initialized. It also has block scope.const PI = 3.14159; console.log(PI); // Output: 3.14159 // PI = 3.14; // This would cause an error because PI is a constant
🧩 Basic Statements
Let's look at some common JavaScript statements:
-
console.log(): This is a very useful statement for debugging and displaying information in your browser's developer console. It prints the value of a variable to the console.console.log("Hello, world!"); -
alert(): This function displays a pop-up alert box in the browser.alert("This is an alert!"); -
document.write(): This function writes text directly into the HTML document. Use this sparingly, as it can be disruptive to the page's structure.document.write("This is written to the page!"); -
function myFunction(argument1, argument2) { ... }: This is a function declaration. It defines a function namedmyFunctionthat takes two arguments and returns a value.function greet(name) { console.log("Hello, " + name + "!"); } greet("Bob");
💡 Tip: Using this
The this keyword is crucial for understanding how JavaScript handles object properties. this refers to the object that the code is currently operating on. It can be set using call, apply, or bind.
const myObject = {
name: "Charlie",
greet: function() {
console.log("This is a greeting from the object.");
console.log(this.name); // Accessing the object's property
}
};
myObject.greet(); // Output: This is a greeting from the object. Charlie
🚀 Practice Exercises
Here are a few exercises to help solidify your understanding:
- Create a JavaScript variable named
countand initialize it to 0. Then, useconsole.log()to print the value ofcount. - Create a JavaScript function called
addthat takes two numbers as arguments and returns their sum. - Create a JavaScript variable named
messageand assign it the string "Hello, JavaScript!". Then, useconsole.log()to print the value ofmessage. - Create a JavaScript function called
greetthat takes a name as an argument and prints a greeting to the console. - Create a JavaScript variable named
myNumberand assign it the value 10. Then, useconsole.log()to print the value ofmyNumber.
These exercises will help you practice the fundamental concepts of JavaScript statements and build a solid foundation for further learning.

