DEV Community

loading...
Cover image for JS Test #4: try/catch

JS Test #4: try/catch

Coderslang: Become a Software Engineer
Teaching you to code at js.coderslang.com - JavaScript, HTML, CSS, Node.js, React.js, React Native
Originally published at learn.coderslang.com Updated on ・1 min read

code-snippet
What will be logged to the console?

.

.

.

.

.

.

.

.

.

.

.

.

.

So, we have 2 variables and 2 try/catch blocks that supposedly catch errors and put them into e1 and e2.

Then, the content of errors is analyzed, compared and the comparison result is logged to the screen.

First, let’s determine what’s inside of e1 and e2. To do that, we need to check the code in the try blocks. Both trying to get to null.length and undefined.length will throw an error as neither undefined nor null have the length property.

These errors will be caught in the catch blocks as e and then assigned to the variables e1 and e2.

The content of these errors will be a bit different. If we were to log e.message to the screen in the catch block, we would see the following:

Cannot read property 'length' of null
Cannot read property 'length' of undefined
Enter fullscreen mode Exit fullscreen mode

Then, .split(' ')[0] gives us the first words of these sentences which is Cannot in both cases. So ultimately, the program can be simplified to:

console.log('Cannot' === 'Cannot')
Enter fullscreen mode Exit fullscreen mode

ANSWER: the expression in the console.log will be evaluated as true and logged to the screen.

Learn Full Stack JavaScript

Discussion (0)