๐Ÿ”’ Members-only lesson โ€” thanks for supporting devnotes
← back to course
progress is saved in this browser โ€” no account needed
devnotes / JavaScript Basics / Lesson 1
lesson 1 of 6 ยท premium

Variables, Data Types & Your First Script

18 min ยท JavaScript Basics

HTML gives a page structure. CSS makes it look good. JavaScript is what makes it do things โ€” react to clicks, update without reloading, remember information. This lesson is where your pages stop being static and start being programs.

What JavaScript actually does

JavaScript runs directly in the browser, on the same page you built with HTML and CSS. It can change text, styles, and structure after the page has already loaded โ€” something HTML and CSS alone can't do on their own. Every dropdown menu, live form validation, and "like" button you've ever clicked was JavaScript running behind the scenes.

Adding JavaScript to a page

Same pattern as CSS in Lesson 2 of the HTML & CSS course: you can write it inline, internally, or in an external file. External is standard for anything beyond a quick test.

<script src="script.js"></script> // placed right before the closing </body> tag, so the page loads first

Variables: storing information with a name

A variable is a labeled container for a piece of data. Modern JavaScript gives you two ways to create one:

let age = 25; // can be reassigned later const name = "Alex"; // cannot be reassigned once set

Use const by default โ€” it's safer because it prevents accidental reassignment. Only use let when you genuinely expect the value to change, like a counter or a running total.

quick tip
You might see older code using var instead of let/const. It still works, but it has confusing quirks that modern JavaScript avoids entirely โ€” there's no good reason to use it in new code today.

The core data types

TypeExampleUsed for
String"hello"Text โ€” always in quotes
Number42, 3.14Any number โ€” no separate type for decimals
Booleantrue, falseYes/no, on/off logic
Array["a", "b", "c"]An ordered list of values
Object{ name: "Alex", age: 25 }Labeled data grouped together

We'll go deep on arrays and objects in Lesson 3 โ€” for now, just recognize them when you see them.

console.log: your best debugging friend

const greeting = "Hello, world!"; console.log(greeting); // prints the value to the browser's console

This is the single most-used line in every JavaScript developer's workflow. It prints a value where you can see it โ€” open your browser's dev tools (right-click โ†’ Inspect โ†’ Console tab) to see it in action. When something isn't working, sprinkling console.log() around your code to see what's actually happening is the standard first move, not a beginner crutch.

AD SLOT โ€” 728ร—90 or responsive (mid-lesson)

Basic operators

const total = 10 + 5; // 15 โ€” math works as expected const combined = "Hi " + "there"; // "Hi there" โ€” + joins strings too const isAdult = age >= 18; // true or false const isEqual = age === 25; // use === not =, to compare values
common mistake
A single = assigns a value. A triple === compares two values. Mixing these up is one of the most common bugs beginners write โ€” if (age = 18) silently sets age to 18 instead of checking it, and doesn't throw an error, which makes it sneaky to spot.

Comments: notes JavaScript ignores

// a single-line comment /* a comment that spans multiple lines */

Try it yourself

exercise
In your Lesson 1 HTML page (from the HTML & CSS course), add <script> tags right before </body>. Inside, create three variables โ€” your name, your age, and whether you're currently learning to code (true/false) โ€” and console.log() each one. Open the browser console and confirm you see all three values.

Level up: build a mini profile

exercise 2
Create a const variable that combines your name and age into one sentence using +, like "Alex is 25 years old", and log it. This is your first taste of building dynamic text from separate pieces of data โ€” exactly what powers things like personalized greetings on real websites.
what's next
Lesson 2 covers functions and control flow โ€” how to make your code actually make decisions (if/else) and repeat actions (loops), instead of just running top to bottom once.
bottom line
Variables store data with let or const, JavaScript has five core data types you'll use constantly, and console.log() is how you see what your code is actually doing. Everything else in this course builds on these basics.
AD SLOT โ€” 300ร—250 or responsive (in-lesson)
next upLesson 2: Functions and Control Flow
Continue โ†’