×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Debug JavaScript Code

JavaScript debugging the code: Here, we are going to learn how to debug the JavaScript using various methods?
Submitted by Siddhant Verma, on October 13, 2019

JavaScript debugging the code

Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and exception handling.

The Strict Mode

JavaScript has an in built strict mode which when used, strictly checks the code for any kind of errors. Some of the flexibility that is usually allowed in the language is barred in the strict mode so that the user writes pure, clean error free code thereby preventing bugs from occuring in the first place. Let's look at some examples,

Example

//function definition
function print() {
    for (i = 0; i < 3; i++) {
        console.log(i);
    }
}

//calling the function
print();

If you run this code, you'll get on the console,

0
1
2

Your code runs perfectly fine producing the correct output even though it had an error. Variable i was not declared anywhere and directly initialized and used. JavaScript reads your code and understands that you must have forgotten to declare i so it does that for you while executing the code. However, running the same code in strict mode,

"use strict";
//function definition
function print() {
    for (i = 0; i < 3; i++) {
        console.log(i);
    }
}

//calling the function
print();

Reports an error saying ReferenceError: i is not defined.

function person(name){
	this.name=name;
}
let fuzzy=person("Fuzzy");

Can you guess what's wrong with the above code?

We have omitted the new keyword before person("Fuzzy") while declaring an object of person type. However, this code runs perfectly fine without producing any errors.

But if we run this in strict mode,

"use strict";
function person(name){
	this.name=name;
}
let fuzzy=person("Fuzzy");

We get an error saying TypeError:Cannot set property of 'name' undefined.

strict mode disallows giving a function multiple parameters with the same name and removes certain problematic language features.

Exception Handling

It a mechanism through which code throws an exception when it runs into a program.

An exception can be any undesired value that we have got because of several reasons. It does not necessarily imply that our code had an abrupt logic or incorrect syntax, it simply means we got something that we didn’t want and it could also be because of interaction with some foreign API. Exception jumps down to the first call that started the current execution therefore unwinding the call stack and throws away all the call context it encounters.

Example

function promptDirection(question) {
    let result = prompt(question);
    if (result.toLowerCase() == 'left') return 'L';
    if (result.toLowerCase() == 'right') return 'R';
    throw new Error('Invalid directions: ' + result);
}

function Look() {
    if (promptDirection('Which way?') == 'L') return 'a house';
    else return 'Two angy beans';
}

try {
    console.log('You see', look());
} catch (err) {
    console.log('Something went wrong ' + err);
}

Output

Something went wrong ReferenceError: look is not defined

throw Keyword

The throw keyword raises an exception and catching is done by wrapping a piece of code in a try block followed by the catch keyword.

JavaScript Tutorial »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.