Try…catch

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

try {

// code…

} catch (err) {

// error handling

}

try...catch only works for runtime errors: For try...catch to work, the code must be runnable. In other words, it should be valid JavaScript.

It won’t work if the code is syntactically wrong, for instance it has unmatched curly braces:

try { {{{{{{{{{{{{

} catch (err) {

alert(“The engine can’t understand this code, it’s invalid”); }

The JavaScript engine first reads the code, and then runs it. The errors that occur on the reading phase are called “parse-time” errors and are unrecoverable (from inside that code). That’s because the engine can’t understand the code.

So, try...catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.

Block Bindings

ECMAScript 6 offers options to make controlling scope easier. This chapter demonstrates why classic var declarations can be confusing, introduces block-level bindings in ECMAScript 6, and then offers some best practices for using them.

Var Declarations and Hoisting: Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting. For a demonstration of what hoisting does, consider the following function definition:

function getValue(condition) {

if (condition) {
var value = "blue";

// other code

return value;
} else {

// value exists here with a value of undefined

return null;
}

// value exists here with a value of undefined
}

Block-Level Declarations: Block-level declarations are those that declare variables that are inaccessible outside of a given block scope. Block scopes are created:

  1. Inside of a function
  2. Inside of a block (indicated by the { and } characters)

Block scoping is how many C-based languages work, and the introduction of block-level declarations in ECMAScript 6 is intended to bring that same flexibility (and uniformity) to JavaScript.

Let Declarations

The let declaration syntax is the same as the syntax for var . You can basically replace var with let to declare a variable, but limit the variable's scope to only the current code block (there are a few other subtle differences discussed a bit later, as well). Since let declarations are not hoisted to the top of the enclosing block, you may want to always place let declarations first in the block, so that they are available to the entire block. Here's an example:

function getValue(condition) {    if (condition) {
let value = "blue";
// other code return value;
} else {
// value doesn't exist here return null;
}
// value doesn't exist here
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reducing Redux: avoiding boilerplate with redux-scc

JSON-to-JSON transformation: A pragmatic approach

Creating a form using Phoenix LiveView since release of v0.16

Introducing TotalRecoilJS: A new developer tool for visualizing your Recoil state

Project setup for a full-stack JavaScript web application

Webpack for Beginners — Part 3

FeathersJS Authentication: typed and tested

Don’t just write APIs DESIGN them!

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
Fatiha Zannat

Fatiha Zannat

More from Medium

Do we really need flatMap ?

.flatMap, think twice, before using …

GUVI-CodeKata

N Queens Problem

Algorithm Practice — Recursive — Solve NQueens problem