Basics of Javascript : Part II

Starting with any language, we need to figure out what elements it contains and what are the basics that we need to get out hands on first. So in Javascript, the basic fundamentals contain things like variables, data types, operators, conditionals (if statements), loops (for and while loops), and functions. These concepts form the building blocks of JavaScript programming.

So here I will explain each of these concepts briefly with an example :

  1. Variables:
  • Variables are used to store and manipulate data in JavaScript. You can declare a variable using the var, let, or const keyword, followed by the variable name.
  • Example: let age = 25;

2. Data Types:

  • JavaScript has several data types, including:
    • Numbers: Represents numeric values, such as 42 or 3.14.
    • Strings: Represents textual data enclosed in single or double quotes, such as 'Hello' or "World".
    • Booleans: Represents true or false values.
    • Arrays: Represents ordered collections of elements enclosed in square brackets, such as [1, 2, 3].
    • Objects: Represents key-value pairs enclosed in curly braces, such as { name: 'John', age: 30 }.
    • Null: Represents the absence of a value.
    • Undefined: Represents an uninitialized variable.
  • Example: let name = 'John';

3. Operators:

  • JavaScript provides various operators for performing operations on data. Some common operators include:
    • Arithmetic operators: Perform mathematical calculations, such as +, -, *, /, %.
    • Comparison operators: Compare values and return a boolean result, such as ==, !=, >, <, >=, <=.
    • Logical operators: Combine multiple conditions and return a boolean result, such as && (AND), || (OR), ! (NOT).
    • Assignment operators: Assign values to variables, such as =, +=, -=, *=, /=.
  • Example: let result = 10 + 5;

4. Conditionals (if statements):

  • Conditionals allow you to execute different blocks of code based on specified conditions. The if statement is the most basic conditional statement.
  • Example:
let age = 18;
if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

5. Loops (for and while loops):

  • Loops allow you to repeat a block of code multiple times.
  • For loop: Executes a block of code for a specific number of iterations.
for (let i = 0; i < 5; i++) {
  console.log(i);
}
  • While loop:
let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

6. Functions:

  • Functions are reusable blocks of code that perform a specific task. They can accept input values called parameters and return a value.
  • Example:
function greet(name) {
  console.log("Hello, " + name + "!");
}

greet("John"); // Output: Hello, John!

So here is basic overview of the fundamentals of Javascript that you can use to understand the basics of this language. Try these examples on your own and see what happens!
The best thing about javascript is that you can directly start writing this code in your browser itself! Just open the Inspect menu from the Right click and start writing code in the console!

Don’t forget to have fun!

Leave a comment