DEV Community

Cover image for How to debug your code in javascript?
kuldeep patel
kuldeep patel

Posted on • Updated on

How to debug your code in javascript?

Hii DEV family 👋

In this article, we are discussing debug your code in javascript sound is good!. Let's start
Debugging your code is very important to solve a problem.
Ya yaa Error...! ðŸĪŊ

Lifelike an error. 😂

Now how to solve it?
Console.log is helping to solve the error.
There are so many types to see the output in the console.

console.log

it's very simple and basic use in javascript programmer...!
ex:1
console.log("ðŸĪŠ");

ex:2
var name = 'Kuldeep';
console.log(name);
If you want to basic way to see an error you can go with it.

But what if we have multiple values that we want to log?

For example:
var name='kuldeep ðŸĪ“';
var friendName='Pawar 😝';
var car = 🏎ïļ;

console.log(name);
console.log(firendName);
console.log(car);

That time we are console.log() 3-time write? No, I have an idea ðŸ’Ą.

I write code like this.
console.log('name'.name, 'My Friend Name'.firendName, 'car'.car);
But this is also long 😆
I write code like this again...
console.log( {name, firstName, car} );

You can write this way your code line is less and you can easily understand to code what is. And You are a smart way programmer.

Let's work with Object:

let car ={
carName: 'BMW',
carColor: 'Black',
price: $250000,
}

console.log(car);
console.log({car});
Enter fullscreen mode Exit fullscreen mode

The first log will print the properties within the car object. The second will identify the object as "car" and print the properties within it.

Variables within the log

console.log('%s is %d years old', "Kuldeep", 29 );

in this example, %s is referred to as string options, and %d is referred to as any number. In this example our output statement is a show like this: Kuldeep is 29 years old.

variants log().

There are a few variant logs. Mostly widely use console.log(). But there is also.

console.info('This is info message') ;
console.warn('Please enter valid number');
console.error('number is not valid');
console.debug('This is debug');
Enter fullscreen mode Exit fullscreen mode

warn print as yellow color and error message print as red color

console.table()

console. table() is view beautiful table view with index number.

let animal = [
    {
        cow: "ðŸŪ",
        cat: "ðŸą"
    },
    {
        dog: "ðŸķ",
        monkey:"🐒"
    }
]
Enter fullscreen mode Exit fullscreen mode

Screenshot_2021-01-05_09-37-19

Top comments (0)