Tag: coding tutorials

  • Day 1 – Warming Up With the Basics of JavaScript

    JavaScript begins with a few simple ideas that form the foundation of everything you’ll build later. Before diving into functions, loops, or DOM manipulation, it’s important to understand how JavaScript handles simple actions like printing output, storing data, and organizing related values. Today’s focus is on these essentials: logging messages, using constants and variables, understanding dynamic typing, working with arrays, and creating basic objects.

    The most fundamental operation is printing information to the console. JavaScript uses the console.log() function to display text or values, which is extremely useful during development. For example, writing console.log("Hello World"); prints the message to the console and helps you confirm that your code is running as expected.

    Another core concept is the difference between constants and variables. A constant is created using the const keyword, and once you assign a value to it, that value cannot be changed. For example, if you write const a = 10;, the number ten becomes fixed for the lifetime of the program. Trying to reassign it will immediately result in an error. This is useful when you want to ensure that a piece of data remains stable.

    Variables, on the other hand, are defined using the let keyword, and they allow you to change the value later. A variable such as let x = 10; can be reassigned to another number, or even to another type entirely. JavaScript is dynamically typed, which means a variable can start as a number and later hold a string like "ABC" without any issues. This flexibility is powerful but also requires attention, especially when dealing with operations like string concatenation. For instance, when a string and a number are added together using the + operator, JavaScript converts the number into a string and joins them, producing "ABC7".

    Arrays are another important building block. They allow you to group values together inside square brackets. An array like [2, 6, 100] stores three numbers, and you can access them using their index, such as arr[1] to retrieve the value 6. If you try to access a position that doesn’t exist—like arr[100]—JavaScript simply returns undefined instead of throwing an error.

    Arrays in JavaScript are also extremely flexible. They can hold values of different types, and they can even contain other arrays inside them. This means an array like [7, true, "abc", [2, 4, 6]] is completely valid. Accessing nested values is straightforward; for example, arr[3][1] retrieves the number 4 from the inner array. This ability to mix and nest values makes arrays ideal for representing structured information.

    Finally, JavaScript provides objects as a way to store related data using key–value pairs. An object such as:

    let obj = {
        name: "Sample Name",
        age: 29
    };

    groups related properties together. Accessing one of these values is done using dot notation—for example, obj.age returns 29. Objects are fundamental in JavaScript, forming the basis for everything from JSON data to browser APIs.

    These simple ideas—logging output, understanding constants and variables, handling dynamic typing, working with arrays, and creating objects—are the building blocks that every JavaScript program grows from. With this foundation in place, the next steps in learning JavaScript become much easier and more intuitive.