Static vs Dynamic Typing

In C#, a variable's type is fixed when it's declared. In JavaScript, variables have no type; values do, and a variable can hold any of them. Declare with const by default, let when the value changes, and never var.

Novice · JavaScript · Updated

After this lesson you can

  • Declare JavaScript variables with const and let, and explain why var is avoided
  • Explain dynamic typing, and check a value's type with typeof

Key takeaways

  • In JavaScript, values have types and variables don't, so the same variable can hold a number now and a string later.
  • typeof gives the type of the value a variable holds right now, such as number, string or boolean. One number type covers whole numbers and decimals.
  • Declare with const by default and let when the value must change. Never use var: it leaks out of its block.
  • const stops a variable being given a new value, but an object or array it holds can still change.
  • An object literal such as { name: 'Larson', age: 25 } makes an object with no class, and you can add properties to it later.

In C#, every variable has a type, decided when it's declared, and checked by the compiler:

int age = 25;
string name = "Larson";

age = "twenty-five";
error CS0029: Cannot implicitly convert type 'string' to 'int'

That's static typing: types are fixed, and checked before the program runs.

Values have types; variables don't

JavaScript has dynamic typing. A variable has no type of its own; it just holds a value, and the value has a type. So the same variable can hold a number now and a string later:

let age = 25;
console.log(typeof age);

age = 'twenty-five';
console.log(typeof age);
number
string

typeof tells you the type of the value a variable holds right now. The main types:

typeof says For values such as Like C#'s
number 25, 3.14, -7 int, double and decimal, all in one
string 'Larson', "Larson" string (single or double quotes both work)
boolean true, false bool
object { name: 'Larson' }, [1, 2, 3], null objects, arrays and lists
undefined a variable with no value yet (nothing like it)
function a function a method or a lambda

JavaScript has just one kind of number, a double. So 0.1 + 0.2 is 0.30000000000000004, as it would be with a C# double. For money, programs count in cents, or round when they display.

Objects without classes

In C#, an object needs a class first. In JavaScript you can write an object straight away, with braces: an object literal.

const person = {
    name: 'Larson',
    age: 25
};

console.log(person.name);
person.age = 26;
person.email = 'larson@example.com';
console.log(`${person.name}, ${person.age}, ${person.email}`);
Larson
Larson, 26, larson@example.com

No class, no constructor: just names and values. You can even add a property, email, that wasn't there before. That looks a lot like the JSON your APIs sent, and that's no accident: JSON stands for JavaScript Object Notation.

(The last line uses a template literal: backticks, with ${...} for each value, just like C#'s quot;..." with {...}.)

Declaring variables: const, let and var

JavaScript has three keywords for declaring variables:

Keyword Means Use it
const Can't be given a new value By default
let Can be given a new value When the value changes, such as a counter
var The old way, with confusing rules Never, in new code
const name = 'Jose';
let count = 0;
count = count + 1;

console.log(`${name} ${count}`);

name = 'Maria';
Jose 1
TypeError: Assignment to constant variable.

Reassigning a const stops the program, but only when that line runs: JavaScript finds most mistakes at run time, not before.

const stops the variable being given a new value, but an object or array it holds can still change. const person = {...} above still let person.age change.

Why never var? A var is visible in the whole function it's declared in, not just its block, so it can leak out of an if or a loop and clash with other code. let and const arrived in 2015 to fix that, and they behave like C# variables: visible only inside their braces.

Check yourself

2 questions to try on the lesson page.

So what

In JavaScript, types belong to values, not variables, and mistakes show up when the code runs. Declare with const unless the value must change, and check with typeof when you need to know what you've got.

Key terms

Cheat sheets