JAVASCRIPT RECAP -2

Avi Mistry
4 min readMay 6, 2021

--

Hi, there. Today I come with JavaScript recap part 2.

JavaScript data types and data structures

Dynamic typing

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 (and reassigned) values of all types:

let foo = 42;    // foo is now a number
foo = 'bar'; // foo is now a string
foo = true; // foo is now a boolean

Data & Structure type

The latest ECMAScript standard defines nine types of data:

Primitive values

All types except objects define immutable values (that is, values that can’t be changed). For example (and unlike in C), Strings are immutable. We refer to values of these types as “primitive values”.

Six Data Types that are primitives, checked by typeof operator:

  • undefined : typeof instance === "undefined"

A variable that has not been assigned a value has the value undefined. See undefined and Undefined for more details.

  • Boolean : typeof instance === "boolean"

Boolean represents a logical entity and can have two values: true and false. See Boolean and Boolean for more details.

  • Number : typeof instance === "number"

ECMAScript has two built-in numeric types: Number and BigInt (see below).

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(253 − 1) and 253 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN ("Not a Number").

  • String : typeof instance === "string"

JavaScript’s String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.

Unlike some programming languages (such as C), JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it.

  • BigInt : typeof instance === "bigint"

The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers.

A BigInt is created by appending n to the end of an integer or by calling the constructor.

  • Symbol : typeof instance === "symbol"

A Symbol is a unique and immutable primitive value and may be used as the key of an Object property. In some programming languages, Symbols are called “atoms”.

Structural Types:

  • Object : typeof instance === "object". Special non-data but Structural type for any constructed object instance also used as data structures: new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date and almost everything made with new keyword;
  • Function: a non-data structure, though it also answers for typeof operator: typeof instance === "function". This is merely a special shorthand for Functions, though every Function constructor is derived from an Object constructor.
  • Structural Root Primitive:
  • null : typeof instance === "object".

Special primitive type having additional usage for its value: if the object is not inherited, then null is shown;

Error handling, “try…catch”

No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, unexpected user input, an erroneous server response, and for a thousand other reasons.

Usually, a script “dies” (immediately stops) in case of an error, printing it to console.

But there’s a syntax construct try...catch that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.

The “try…catch” syntax

The try...catch construct has two main blocks: try, and then catch:

try {  // code...} catch (err) {  // error handling}

It works like this:

  1. First, the code in try {...} is executed.
  2. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch.
  3. If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err). The err variable (we can use any name for it) will contain an error object with details about what happened.

So, an error inside the try {...} block does not kill the script – we have a chance to handle it in catch.

Error Object

When an error occurs, JavaScript generates an object containing the details about it. The object is then passed as an argument to catch:

try {
// ...
} catch (err) { // <-- the "error object", could use another word instead of err
// ...
}

For all built-in errors, the error object has two main properties:

name Error name. For instance, for an undefined variable that’s "ReferenceError".messageTextual message about error details.

There are other non-standard properties available in most environments. One of most widely used and supported is:

stackCurrent call stack: a string with information about the sequence of nested calls that led to the error. Used for debugging purposes.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Avi Mistry
Avi Mistry

Written by Avi Mistry

HI I'm Avi Mistry . A full stack web developer works with ReactJs, NodeJs, MongoDB, Firebase, Netlify & more ;)

No responses yet

Write a response