30 Days of JavaScript: JavaScript Syntax and Core Concepts, Day 2

30 Days of JavaScript: Building a Simple Weather App
30 Days of JavaScript: JavaScript Syntax and Core Concepts, Day 2

Explore JavaScript basics in Day 2 of our journey, focusing on variables, data types, operators, loops, and scope. Perfect for building a strong coding foundation.

Day 2 is where JavaScript stops feeling abstract. Almost everything you write later leans on a handful of ideas: variables, data types, operators, conditionals, loops, functions, and scope. Get these solid now and the harder stuff has somewhere to land. We’ll keep it short and lean on the code.

Table of Contents

A Closer Look at Variables, Data Types, and Operators

Variables

A variable is how you store a value and reach for it again later. You declare one with let or const, then give it a name. The two behave differently, and the difference matters:

  • let lets you reassign the value later.
  • const can’t be reassigned once you’ve set it.

Here’s the basic shape:

JS
/**
 * Declares a variable and a constant
 * @type {undefined}
 */
let ourVariable;
const ourConstant = 42;

So ourVariable can change later, while ourConstant stays at 42. One honest caveat: const stops you rebinding the name, not mutating what’s inside. A const object or array can still have its contents changed. You just can’t point the name at something new.

You’ll also run into var in older code. It’s function-scoped and hoists differently, which is exactly the behavior let and const were added to fix. Reach for those two and leave var to legacy files.

Data Types

Every value in JavaScript has a type. Seven of them are primitives (string, number, boolean, null, undefined, symbol, and bigint), and then there’s the object, which holds everything more complex. Here are the ones you’ll meet first.

  • String: text.
JS
/**
 * Declares a string variable
 * @type {string}
 */
let str = "Hello, world!";
  • Number: integers and floating-point numbers, same type for both.
JS
/**
 * Declares a floating-point number variable
 * @type {number}
 */
let float = 3.14;
  • Boolean: true or false.
JS
/**
 * Declares a boolean variable
 * @type {boolean}
 */
let isJavaScriptFun = true;
  • Null: a value you set on purpose to mean “nothing here.”
JS
/**
 * Declares a variable with null value
 * @type {null}
 */
let emptyValue = null;
  • Undefined: a variable that’s been declared but never given a value.
JS
/**
 * Declares an uninitialized variable
 * @type {undefined}
 */
let uninitializedVar;
  • Object: structured data, like the fields of a record or the items in a list.
JS
/**
 * Declares an object representing a person
 * @type {Object}
 */
let person = {
    firstName: "John",
    lastName: "Doe",
    age: 30
};
  • Symbol: a unique, immutable value, often used as an object property key you know won’t clash.
JS
/**
 * Declares a unique symbol
 * @type {symbol}
 */
let uniqueID = Symbol("id");

One trap worth flagging early: typeof null returns "object", not "null". It’s a long-standing quirk in the language, so when you need to check for null, compare with === null directly.

Operators

Operators are how you act on values. Three groups cover most of what you’ll do day to day.

Arithmetic Operators

The math ones:

  • +: adds.
  • -: subtracts.
  • *: multiplies.
  • /: divides.
  • %: remainder of a division.
  • **: exponent, raises a number to a power.
JS
/**
 * Performs various arithmetic operations
 * @type {number}
 */
let sum = 10 + 5; // 15
let product = 10 * 5; // 50
let remainder = 10 % 3; // 1
let power = 2 ** 3; // 8

Comparison Operators

These compare two values and hand back a boolean:

  • ==: equal to.
  • ===: strict equal, checks value and type.
  • !=: not equal to.
  • !==: strict not equal, checks value and type.
  • <, >: less than, greater than.
  • <=, >=: less than or equal, greater than or equal.
JS
/**
 * Compares two values using comparison operators
 * @type {boolean}
 */
let isEqual = (5 == "5");  // true (only compares values)
let isStrictEqual = (5 === "5"); // false (compares value and type)

The plain == converts types before it compares, which is how 5 == "5" comes out true. That coercion causes more bugs than it saves. Default to === and reach for == only when you actually want the loose behavior.

Logical Operators

These combine or flip boolean values:

  • &&: AND, true when both sides are true.
  • ||: OR, true when at least one side is true.
  • !: NOT, flips true to false and back.
JS
/**
 * Performs logical operations on boolean values
 * @type {boolean}
 */
let andCondition = (5 > 3 && 10 > 6); // true
let orCondition = (5 > 10 || 10 > 6); // true
let notCondition = !(5 > 10); // true

Mastering Conditional Statements and Loops

Conditional Statements

Conditionals run a block of code only when a condition is true. The workhorse is if...else:

JS
/**
 * Demonstrates an if-else conditional structure
 * @param {boolean} condition - The condition to evaluate
 */
if (condition) {
  // Code executed if the condition is true
} else {
  // Code executed if the condition is false
}

That’s how your code makes decisions off whatever data it’s handed at runtime.

Loops

Loops repeat a block of code. You’ll reach for a for loop when you know how many times to run, and a while loop when you’re waiting on a condition to flip:

JS
/**
 * Loops through numbers from 0 to 9 using a for loop
 */
for (let i = 0; i < 10; i++) {
  // Code executed during each iteration
}
/**
 * Repeats code as long as the condition is true using a while loop
 */
while (condition) {
  // Code executed as long as the condition is true
}

Delving into Functions and Scope

Functions

A function is a named block of code you can call whenever you need it, with different inputs each time. Declare one with the function keyword:

JS
/**
 * Greets a person with a message
 * @param {string} name - The name of the person to greet
 * @returns {string} - The greeting message
 */
function greet(name) {
  return "Hello, " + name + "!";
}
greet("John"); // Outputs: "Hello, John!"

Scope

Scope is about where a variable is reachable. Two ideas cover the basics:

  • Global scope: declared outside any function or block, reachable anywhere.
  • Local scope: declared inside a function or block, reachable only in there. With let and const, that boundary is the nearest { } block, not just the function.
JS
/**
 * Demonstrates global and local scope in JavaScript
 */
let globalVar = "I am global";
function myFunction() {
  let localVar = "I am local";
  console.log(globalVar);  // Accessible
  console.log(localVar);   // Accessible
}
myFunction();
console.log(localVar);  // Unaccessible, throws an error

That last line fails on purpose. localVar only lives inside myFunction, so the outside world can’t see it. Keeping variables local like this is how you avoid names colliding as a program grows.

Conclusion

That’s the core of the language in one sitting: variables to hold data, types to describe it, operators to act on it, conditionals and loops to control the flow, functions to package logic, and scope to keep it all from stepping on itself. None of it is complicated on its own. The skill is combining them.

If one thing sticks, make it this: prefer const and let over var, and prefer === over ==. Those two habits quietly prevent a whole class of bugs before you ever hit them.

What’s Next?

Day 3 gets hands-on with arrays, objects, and events, the pieces you’ll use to actually move data around and respond to what a user does.

Next: 30 Days of JavaScript: In-Depth Exploration of Arrays and Objects in JavaScript, Day 3

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top