Home

try..catch

First, the code in try {…} is executed. If there were no errors, then catch(err) is ignored: the execution reaches the end of try and goes on, skipping catch. 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 e...

Read more

throwing and rethrowing our own errors

let json = '{ "age": 30 }'; // incomplete data try { let user = JSON.parse(json); // <-- no errors if (!user.name) { throw new SyntaxError("Incomplete data: no name"); // (*) } alert( user.name ); } catch(e) { alert( "JSON Error: " + e.message ); // JSON Error: Incomplete data: no name } rethrowing errors: let json = '{...

Read more

Global error catch

In the browser we can assign a function to the special window.onerror property, that will run in case of an uncaught error. Node.js has process.on(“uncaughtException”) for this. Syntax: window.onerror = function(message, url, line, col, error) { // ... }; Example: <script> window.onerror = function(message, url, line, col, erro...

Read more

Alert shows undefine when passed object

Alert takes string as an input paramter. We need to stringify our object. Example: let salaries = { John: 100, Ann: 160, Pete: 130 };MultiplyNumeric(salaries);function MultiplyNumeric(obj) { for (let key in obj) { if (obj.hasOwnProperty(key)) { obj[key] *= 2; } } }alert(JSON.stringify(salaries));

Read more

Variables

We can declare variables to store data by using the var, let, or const keywords. let – is a modern variable declaration. var – is an old-school variable declaration. Normally we don’t use it at all, but we’ll cover subtle differences from let in the chapter The old “var”, just in case you need them. const – is like let, but the value of the var...

Read more

Comparisons

Comparison operators return a boolean value. Strings are compared letter-by-letter in the “dictionary” order. When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check). The values null and undefined equal == each other and do not equal any other value. Be careful when using compari...

Read more

In-browser javascript limitations

JavaScript’s abilities in the browser are limited for the sake of the user’s safety. The aim is to prevent an evil webpage from accessing private information or harming the user’s data. Examples of such restrictions include: JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It h...

Read more

In-browser javascript capabilities

Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it. JavaScript’s capabilities greatly depend on the environment it’s running in. For instance, Node.js supports functions that allow JavaScript to read/write arbitrary file...

Read more