More JavaScript

Numbers and strings are called primitive values.

Let’s watch these examples:

All the primitive values have one thing in common. There is not much to do in code to affect them.

Objects and functions are also values but they are not primitive which makes them special. Let’s see these examples:

Now if we look at the browser console, we can see that the console displays them differently. Objects and functions are special because we can manipulate them from code.

If we want to check a value’s type, we can ask it with the typeof operator. JavaScript will answer whether it is a number, string or object.

Sometimes we have errors in JavaScript. Usually, a script immediately stops in case of an errors, printing it to console. But there’s a syntax construct that allows us to catch errors so the script can, instead of dying, do something reasonable.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store