Types in JavaScript

Ramazan Ramazanov
2 min readFeb 1, 2021

Programming languages all have built-in data structures, but these often differ from one language to another. JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned values of all types.

Statically typed means the type is enforced and won’t change so easily. All variables must be declared with a type. Static typing is exemplified by C++ and Java’s use of variable declaration.

int a == 100 // explicitly declares the value type as integer

Dynamically typed languages infer variable types at runtime. This means once your code is run the compiler/interpreter will see your variable and its value then decide what type it is. The type is still enforced here, it just decides what the type is. Dynamic typing is exemplified by JavaScript’s use of “var, const, let” to declare variables. The compiler takes responsibility for determining the type of the value:

let a = 100 // compiler determines it is an integer

The difference between strong and weak typings is that strong typing does not allow different value types to interact, but weak typing does allow that, coercing an integer into its string equivalent when interacting with a string.

Strong Typing:

var hello = ‘hello’
hello + 17 // ERROR

Weak Typing:

var hello = ‘hello’
hello + 17 // ‘hello17’

Weakly typed languages allow types to be inferred as another type. For example, 1 + ‘2’//’12' in JS it sees you’re trying to add a number with a string — an invalid operation — so it coerces your number into a string and results in the string ‘12’.

Primitive Types

A primitive is not an object and has not methods of its own. All primitives are immutable.

  • Boolean — true or false
  • Null — no value
  • Undefined — a declared variable but hasn’t been given a value
  • Number — integers, floats, etc
  • String — an array of characters, i.e words

--

--