Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

The Concept of Data Types

In programming, understanding data types is crucial.

It enables effective manipulation of variables, ensuring that operations can be performed safely and accurately.

Without data types, a computer would struggle to reliably execute tasks.

let x = 16 + “Volvo”;

When attempting to add “Volvo” to sixteen in JavaScript, the operation will be interpreted as:

let x = “16” + “Volvo”;
Note: JavaScript treats the number as a string when adding a number and a string.

Example

let x = 16 + “Volvo”;

Example

let x = “Volvo” + 16;

The evaluation of expressions in JavaScript follows a left-to-right sequence, which can yield varying results depending on the order of operations.

JavaScript:

let x = 16 + 4 + “Volvo”;

Result:

20Volvo

JavaScript:

let x = “Volvo” + 16 + 4;

Result:

Volvo164

In the first example, JavaScript interprets 16 and 4 as numbers until encountering “Volvo”.

In the second example, because the first operand is a string, all subsequent operands are treated as strings as well.

JavaScript Types are Dynamic

In JavaScript, dynamic typing allows a single variable to accommodate different data types interchangeably.

Example

let x;       // Now x is undefined
x = 5;       // Now x is a Number
x = “John”;  // Now x is a String