Jan 28, 2026 • 5 min read

variables

What I learned, what I’m still figuring out, and where I might go next.


The basic data types in JavaScript are:

  • boolean: a boolean value, either true or false
  • string: a sequence of characters
  • number: represents both integer and floating-point (fractional) numbers
  • undefined: a variable that has not been assigned a value search engine

Declaring Variables

There are better ways to declare variables - but this is a basic examples for the data types above.

var isTall = true;
var isCool = false;
var name = "Alice";
var age = 42;
var weight = 150.5;

var brainSize = undefined;

The undefined value is a special value in JavaScript that represents the absence of a value.

The better way to declare variables is to use let (for variables that can be reassigned) or const for static variables that are not expected to change.

Why avoid var?

The issue with var is that it’s function-scoped, not block-scoped, which leads to unexpected behavior.

if (true) {
  var aNumber = 42;
}
console.log(aNumber); // 42

Save the headache and just use let and const.

if (true) {
  let aNumber = 42;
  const aConstNumber = 43;
}
console.log(aNumber); // ReferenceError: aNumber is not defined
console.log(aConstNumber); // ReferenceError: aConstNumber is not defined

Numbers in JavaScript

In Python, numbers without a decimal part are called Integers and fractions are floats. Contrast this to JavaScript where all numbers are just a number type.

let x = 2; // this is a number
x = 5.69; // this is also a number
x = -5.42; // yup, still a number

This means you can also do arithmetic with them.

let sum = 2 + 3 + 7; // 12
let difference = 5.3 - 2.1; // 3.2
let product = 2 * 3; // 6
let quotient = 6 / 2; // 3

Increment and Decrement

In Python there is += operator to increment a number. That operator works in JavaScript as well, but JS also has a ++ operator for when you only want to increment by 1.

let startingNumber = 4;
startingNumber++;
console.log(startingNumber); // 5
startingNumber += 5;
console.log(startingNumber); // 10

And there is a -- operator for decrementing by 1.

let startingNumber = 11;
startingNumber--;
console.log(startingNumber); // 10
startingNumber -= 5;
console.log(startingNumber); // 5

Undefined vs Undeclared

The primitive undefined value represents the absence of a value… but it’s not the same thing as an “undeclared” variable.

You can create an undefined variable by giving it a name, but no value:

let myName; // undefined
console.log(typeof myName); // "undefined"

If we never create the name, it’s undeclared, and undeclared variables throw an error.

console.log(myName); // ReferenceError: myName is not defined

It’s confusing because JS says that the undeclared variable is “not defined”. Makes sense…

Null vs. Undefined

JavaScript actually has two values for “nothing”:

  • undefined: It doesn’t exist at all. undefined is “very nothing”
  • null: It (kind of) exists, but it’s emptynull is “kinda nothing”

There are some practical differences between the two, the primary one being that undefined is the default value of a variable when it hasn’t been given a value yet.

let myName;
console.log(myName); // undefined

To get a null value, you have to explicitly assign it:

let myName = null;
console.log(myName); // null

In my experience I just go with undefined, anywhere I would use None in Python or Nil in Go.

Dynamic and Weak

Like Python, JavaScript is a dynamically-typed (not statically-typed) language. Unlike Python, it’s also weakly-typed, meaning it will automatically convert types when you do things like add a number to a string (concatenation).

Strings

In JavaScript, a (non-template) string can be written with either single or double quotes. For example:

  • 'Hello'
  • "Hello".

I prefer double quotes.

> It's important to have styling conventions so that all the code in a project looks consistent, making it easier to read and contribute to.

Indexing

Square brackets are used to access individual characters inside a string. The characters are numbered from 0 to length-1. It’s similar to how strings and lists work in Python, Go and many other languages.

const greeting = "Hello";
greeting[0]; // 'H'
greeting[1]; // 'e'
greeting[2]; // 'l'
greeting[3]; // 'l'
greeting[4]; // 'o'
// you can also get the last char at length-1
greeting[greeting.length - 1]; // 'o'

The .length property is used to get the number of characters in a string.

Template Literals

In JavaScript, template literals are a fantastic way to interpolate dynamic values into a string. They’re JavaScript’s version of Python f-strings. For example:

const number = 101;
console.log(`The value of number is ${number}`);
// The value of number is 101

Template literals must start and end with a backtick, and anything inside of the dollar-sign bracket enclosure is automatically cast to a string.

We aren’t limited to just variable names inside the ${}. We can actually write valid JavaScript code directly inside them.

This means you can do math, change text styles, or run logic checks right inside the string.

const price = 2.0;
const quantity = 5;
const item = "candy";

console.log(`Total: ${price * quantity}`);
// Total: $10.0

console.log(`I want ${item.toUpperCase()}!`);
// I want CANDY!

Still Curious About