Page Content

Tutorials

Level Up Your JavaScript Simple Programs for Core Concepts

JavaScript Simple Programs

Writing a basic JavaScript program requires integrating a number of basic building elements that are introduced. Data types, variables, operators, statements, expressions, control flow (logic statements and loops), and functions are a few examples. A computer program basically consists of a collection of instructions that a computer is supposed to obey.

Among the first building blocks you come upon are variables. In your programming, variables are used to store values. They have names and can have a variety of values. Variables are declared using keywords like as let, var, or const.

Here’s a basic illustration of how to declare variables and give them values:

let score = 0; // Declares a variable named score and assigns it the value 0
const maxScore = 100; // Declares a constant variable named maxScore
let username = “Alice”; // Declares a string variable

Variables hold various kinds of data. Booleans (true or false), integers, and strings (text) are examples of primitive data types. One variable can hold several values with composite data types like arrays and objects.

Statements are JavaScript commands that carry out operations. An assortment of assertions is called a program. You can use semicolons to separate statements. Parts of statements that generate values are called expressions. A literal value, a variable reference, or a mix of values and operators can all be used.

Values can have actions applied to them via operators. Arithmetic operators, for instance, carry out computations, comparison operators contrast values, and logical operators merge Boolean values.

Let’s put statements, operators, and variables together:

let x = 5; // Statement declaring variable x and assigning value 5
let y = 10; // Statement declaring variable y and assigning value 10
let sum = x + y; // Expression (x + y) evaluated, result stored in sum by assignment operator
console.log(sum); // Statement calling the console.log function to output the value of sum

The expression x + y, the sentence let x = 5;, and the statement console.log(sum); all invoke built-in functions in this example.

In most cases, code runs from top to bottom. Statements of control flow let you change this sequential execution. Loops and conditional expressions are examples of this.

Conditional statements provide your code the ability to decide what to do and take alternative actions depending on certain circumstances. The following conditional statements are frequently used: if, else if, and else. The switch statement has the ability to compare a single condition with several scenarios. A shortcut for basic if-else sentences is the ternary operator.

Using if, else if, and else, here’s an example:

let age = 15; // Variable to store age
let message; // Variable to store message

// Check if age is less than 12
if (age < 12) {
message = “You are a child.”;
}
// If not less than 12, check if less than 18
else if (age < 18) {
message = “You are a teenager.”;
}
// If neither condition is true (age is 18 or older)
else {
message = “You are an adult.”;
}

// Output the determined message to the console
console.log(message);

This code illustrates how the value of the age variable determines which code blocks are run.

Multiple code blocks are repeated using loops. Loops come in a variety of forms, such as while and for. A loop keeps going as long as a specific condition is met.

This is an illustration of a for loop:

for (let i = 0; i < 5; i++) { // A for loop that runs 5 times
console.log(“Iteration number: ” + i); // Output the current iteration number
}

This loop increments a counter i after each execution, starting it at 0 and continuing as long as i is less than 5.

Last but not least, functions let you combine several statements into a single, reusable body of code. This aids in preventing self-repeat. When necessary, you can call or invoke your own functions that you have defined. Functions can return values and accept parameters, or input values.

An illustration of defining and invoking a basic function is shown here:

function greet(name) { // Defines a function named greet that takes one parameter, name
let greeting = “Hello, ” + name + “!”; // Creates a greeting message;
console.log(greeting); // Outputs the greeting;
}

greet(“World”); // Invokes the greet function with the argument “World”);
greet(“Alice”); // Invokes the greet function again);

In order to show reusability, this program defines the function greet and then calls it twice under various names.

Variables store data, operators alter it, statements perform actions, control flow directs execution, and functions organise code to construct simple JavaScript programmes that solve problems or complete tasks. Console.log() is important for beginners because it displays code output and helps you comprehend it.