Static vs Dynamic Typing

How JavaScript's dynamic typing differs from Java's static typing, and when to declare a variable with const, let or var.

Novice · JavaScript · Updated

By

Java is a statically typed language: every variable has a type that is fixed when you declare it. JavaScript is dynamically typed: a variable can hold any type of value, and the type can change.

In Java, when a variable is declared, you must define its data type. The data type of that variable can never change.

int age = 25;
String name = "Larson";
Person person = new Person("Larson", 25);

In JavaScript, however, we cannot specify the data type in the declaration; instead, the JavaScript runtime infers the type of the variable based on its current value.

// age is a number because 25 is inferred to be a number
let age = 25;

// name is a string because its value is in quotes
let name = 'Larson';

// person is a dynamic object that may contain any attributes we define
let person = {
            name: 'Larson',
            age: 25
          };

In JavaScript, variables can be re-defined to hold different data types than when they were first initialized. For example, a variable can go from storing a number to storing a string or from storing an object to storing a string.

// age can be redefined a string
age = 'twenty-five';

// person can be re-defined as well
person = 'Larson';

Declaring Variables

JavaScript provides three keywords to declare variables: var, let, and const.

var is the original keyword. A var variable belongs to the whole function it is declared in (or is global when declared outside any function), not to the block between its braces, which leads to surprising bugs.

In 2015, ECMAScript 6 was released and introduced 2 new variable declaration keywords. These keywords allow greater control over variable scope.

You should avoid using var in new code. It is only still available for backwards compatibility with existing code.

let declares a variable whose value can be changed later.

const declares a variable that can't be reassigned. This is the preferred declaration and should be your default; change it to let when you find the variable needs a new value. (If a const holds an object or array, its contents can still change.)

// declares a function-scoped variable named age (global here, outside any function)
var age;

// declares a scoped variable that can be changed
let price;

// declares a block-scoped variable that cannot be reassigned
// this should be your default declaration type
const name = 'Jose';

Do it: write the snack stand's first snack

The snack stand's menu script needs its first snack. In Java that would need a Snack class first. In JavaScript, write the object straight away, and let a variable hold whatever type of value the job needs.

After your code runs, the program holds two variables:

  • snack: the object { name: 'Trail Mix', price: 5 }.
  • stock: the text 'sold out'.

Each one gets there in two steps. The snack starts at a price of 4, then its price goes up to 5. The stock starts as the number 12; then the stand sells out, and the same variable holds the text 'sold out' instead.

Your job: write four short lines.

  • Declare snack with const, holding an object with two properties: name, 'Trail Mix', and price, 4.
  • On a line of its own, change the snack's price to 5. (A const can't be given a new object, but the object it holds can change.)
  • Declare stock with let, holding the number 12.
  • On a line of its own, give stock the text 'sold out'.

Practiced live on the lesson page: write the JavaScript right there and check it.

Cheat sheets