Sitemap
StartCode

StartCode is a publication dedicated to all kind of programming enthusiasts

Javascript Basics: Data Types — Part 1

6 min readSep 28, 2019

--

Press enter or click to view image in full size

Definition: The data type of a variable tells the interpreter (or the compiler) how we intend to use that variable.

If we would like to categorize programming languages based on data types we can split them in two categories: statically typed and dynamically typed. Javascript is part of the second category and that makes the development process easy and fast. However there is a downside of this aspect when comes to bug fixing and debugging process (it’s harder to understand your code when you look at it 3 weeks later if you didn’t added comments and respected clean code principles).

If you are interested in other topics related to Javascript Basics please check them out:

What are the data types supported by Javascript?

  • Primitives: undefined, null, Number, String, Boolean, BigInt, Symbol;
  • Objects

Primitives

Definition: A primitive is represented by data that is not an object and contains no methods.

1.1 undefined — data type

Note: Don’t explicitly initialize a variable with undefined as a developer. You have null!

Definition: Undefined is a data type used by Javascript engine in order to tell the developer that there is no memory allocated for a piece of data or a variable has not been initialized.

What does that mean?

Example 1:

Fig.1.1.1 Variable is not defined error

In this example we are trying to print in the browser’s console a variable named b but that variable is not declared anywhere in our Javascript file. The interpreter is smart enough to notice that and throws a ReferenceError with a specific message.

Example 2:

Fig.1.1.2 Variable of type undefined

The difference between first and second example is at the line 5 where we added the declaration of variable b. Our script is not throwing an error anymore and the engine is telling us that b is declared but there is no value associated with it (undefined).

Example 3 (hoisting mechanism):

Fig.1.1.3 Hoisting and undefined

If we are taking a look at the 1st line of code we’re trying to print at the console the value of variable b, you would expect to see an error thrown because we are trying to access a piece of data that wasn’t declared and initialized yet.

Fig.1.1.4 Global variable window

By analyzing the global variable window before we are trying to run our code, we can notice that variables a and b exists in memory but there are no values associated with them.

Why is this happening?

Javascript interpreter analyzes our code and allocates memory for each variable found in the global execution context. The variables won’t be initialized until the execution reach the specific lines of code (in our example line 5 and 6). This behavior is different when it comes to functions but this aspect will be covered in another article.

1.2 null — data type

Definition: Null data type is the equivalent of undefined but specific to developers. In order to declare a variable and tell the engine that it doesn’t contain any value, we initialize it with null.

Example:

Fig.1.2.1 Null usage

In this example we take two variables a and b initialized with undefined and null. On the line 7 it can be noticed that the javascript engine treats the two values the same that’s why the printed value is true. When the check for data type is performed (line 8) the result is evaluated to false.

1.3 Number — data type

Definition: Javascript treats floating point values and integers as a single data type called number that is the equivalent of double’s size (64 bits).

Example 1:

Fig.1.3.1 Basic number usage

As it was mentioned above, numbers are treated as a single data type and offers a mechanism to parse strings to integers or floats.

Example 2:

Fig.1.3.2 Number boundaries

Constant values are provided for checking the boundaries supported by the language for integers and floating point numbers:

  • MIN_SAFE_INTEGER & MAX_SAFE_INTEGER: min and max value supported by a number used as an integer;
  • MIN_VALUE & MAX_VALUE: min and max value supported by a number used as floating point numbers;

Note: Don’t make a confusion between the two!

Example 3:

Fig.1.3.3 Number special values

The language provides a set o special values: NaN, Infinity and -Infinity in order to prevent bugs from computation of too big numbers or non numeric parsing.

Note: NaN === NaN is false!

1.4 String — data type

Definition: Just like any other programming language String data type is used to represent a sequence of characters.

Get Cartas Cosmin’s stories in your inbox

Join Medium for free to get updates from this writer.

Example 1:

Press enter or click to view image in full size
Fig.1.4.1 Basic string initialization

We can initialize string variables by using string literals (direct initialization using ‘’ or “”) or by using String data type.

Example 2:

Press enter or click to view image in full size
Fig.1.4.2 String concatenation

String concatenation can be done with different data types by using + operator and the coercion is done automatically. Not that for Symbols you cannot perform direct concatenation and toString() method has to be called.

Example 3:

Press enter or click to view image in full size
Fig.1.4.3 String interpolation

Having multiple variables that needs to be concatenated into a single string could lead to unreadable code. String concatenation was made easier from the introduction of string interpolation in ES6 (using back ticks `` and ${} for variable values).

1.5 Boolean — data type

Definition: Boolean is one of the most important data types from the language and is used for evaluating expressions to true or false.

Example:

Fig.1.5.1 Boolean used with different data types

Iterating through to all data types used in this example we can notice the following behavior:

  • Number — Initializing a Boolean variable with any non-zero value is evaluated to true;
  • String — Initializing a Boolean variable with anything different than an empty string is evaluated to true;
  • Null & Undefined — Boolean evaluates null and undefined to false;
  • BigInt — Boolean evaluates any value different than 0n to true (not presented in the example);
  • Symbol — Boolean evaluates any symbol to true;

1.6 BigInt — data type

Definition: BigInt is a new primitive data type that allows initialization of variables with values bigger than the MAX_SAFE_INTEGER or smaller than MIN_SAFE_INTEGER.

Example:

Fig.1.6.1 Basic BigInt usage

BigInts have a ’n’ appended after the numeric value. Even though BigInt is a numeric data type, they cannot be used with the current Math library or with the coercion mechanism applied to basic operations with numbers. (line 5 and 6).

1.7 Symbol — data type

Definition: Symbol is a type introduced in ES6 that provides a mechanism to create unique identifiers. They provide an argument used as a descriptor for that specific symbol.

Their true purpose is to do meta-programming but this will not be covered in the basics topics.

Example 1:

Fig.1.7.1 Basic Symbol Usage

Note that symbols are primitives just like the other data types presented above and cannot be used with the new keyword or with a shorthand initialization.

Even though we initialize two symbols with the same descriptor, they are represented as two different values (see line 6).

Example 2 (objects):

Press enter or click to view image in full size
Fig.1.7.2 Object with no encapsulation

If we would like to create an object with some sensitive property like the IBAN is, the desired behavior would be to prevent the modification of the property outside of the class. In this example the IBAN is changed without any problems as it can be noticed in the printed values from the console.

Example 3 (objects and symbols):

Press enter or click to view image in full size
Fig.1.7.3 Object encapsulation using Symbol

We can achieve the encapsulation of BankAccount with the definition of IBAN as a Symbol and manipulate its value inside the object. The property is not visible when we run Object.entries or Object.keys. Unfortunately the property is not fully private since it can be accessed by using Object.getOwnPropertySymbols.

Note: Symbol properties can be modified by using reflection mechanism!

In the second part of this article we will present how Object data type works in Javascript.

If you have any questions related to this article please leave a comment below. Subscribe to our publication in order to find more articles like this and enhance your developer skills on various topics. Thanks!

--

--

StartCode
StartCode

Published in StartCode

StartCode is a publication dedicated to all kind of programming enthusiasts