Programming is the process of writing instructions that a computer can understand to perform a specific task. With programming, we can create applications, websites, systems, and various other software that are useful for everyday life.
Variables are used to store data that can be accessed and manipulated while the program is running.
// Declare variables
let name = "Ali"; // String type variables
let old= 25; // Number type variables
let isStudent = true; // Boolean type variables
// Access and manipulate variables
console.log("Name:", name ); // Output: Name: Ali
console.log("Old:", old); // Output: Old: 25
console.log("Student:", isStudent); // Output: college student: true
// Modify variable values
name = "Budi";
age = 26;
isStudent = false;
console.log("New Name:", name); // Output: New Name: Budi
console.log("New Age:", age); // Output: New Age: 26
console.log("Student:", isStudent); // Output: Student: false
Conditions help programs make decisions based on certain logic,
such as using if
or switch
.
let age = 20;
if (age < 18) {
console.log("You are not old enough to get a driver's license.");
} else if (age >= 18 && age < 21) {
console.log("You are old enough to get a driver's license.");
} else {
console.log("You are an adult and can do many things!");
}
let day = "Monday";
switch (day) {
case "Monday":
console.log("Today is the start of the work week.");
break;
case "Friday":
console.log("Today is the end of the work week.");
break;
case "Saturday":
case "Sunday":
console.log("Weekend! Time to relax.");
break;
default:
console.log("Regular day.");
}
Looping allows a program to repeat a certain process using
for
or while
.
// Using for loop to print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log("Number:", i);
}
// Use while loop to print numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log("Number:", i);
i++; // Increment value i
}
A function is a block of code designed to perform a specific task, so it can be reused in different parts of a program.
// Define a function to display a greeting
function sapa(name) {
console.log("Hello, " + name + "! Welcome.");
}
// Call the function
sapa("Ali");
sapa("Budi");
Programming is an essential skill in today's digital age. By understanding the basics of programming, you have begun your journey to create something useful and inspiring the world. Don't be afraid to learn and keep growing!