Gemini AI inside Chrome DevTools to Debug Errors
Tags: ai, debug, javascript, productivity
Published: 2024-07-11 12:01:08

## Introduction

Gemini, formerly known as Bard, is a generative artificial intelligence chatbot developed by Google. 
(Source: Wikipedia)

TLDR: Gemini is another ChatGPT but by Google :) 

In this blog, we will be discussing about Gemini - not the usual one but the one that is now integrated in to Chrome DevTools and help us debug right in there; instead of copy pasting the code from console to some AI chatbot like Gemini, ChatGPT or GitHub Copilot Chat.

This feature is called as **Understand console messages with AI**

Official Documentation: https://developer.chrome.com/docs/devtools/console/understand-messages

## Video Explanation
I have made a short and quick video for this explanation:

{% youtube WVSlgxLMojk %}

## Enable the feature

In order for you to enable this feature, you will have to:
- Open Chrome DevTools (Press F12)
- Go to Settings > Preference > Console 
- Turn on **"Understand console messages with AI"**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdrghgy1fzhtanqbd3qj.png)

## Understand Console Messages with AI

Now the magic is somewhat similar as what we have been experiencing since the introduction of such tools. 

Lets take a look at it.

Open a Chrome DevTool on any webpage, preferably an empty page and clear the logs if there are any.

You can clear the logs by pressing "CTRL + L" or by clicking at top left (second icon) as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5nx6a6fyeep9jekbcv5.png)


Now type a code like this:

```js
const a = 1
a = 2
```

This should cause an error in the console like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mimatehxk4kmqf42noo0.png)

If you hover over the error, you will find an icon of a light bulb at top right:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9r9ddsung605d82y50z.png)

As you hover over it, it'll expand and show you "Understand this error":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d71r6q99cgdt8mu1j1f.png)

After you click on it, you will be asked about your permission to send the data to Google to understand the context:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dp0vnthc3cr0znnrytu9.png)

Once you click accept, you will be provided with the explanation of the error and potential solution for that as well:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/397ec6v0s1avi7nlx3xa.png)

### Explanation and Solution by Gemini in Console

Following is the explanation and solution provided by the Gemini when I tried this for writing the blog:

The error "Uncaught TypeError: Assignment to constant variable" means you're trying to change the value of a variable that was declared as a constant using either const or (in some cases) let within a block where it's already defined.

Here's why you're getting the error and how to fix it:

1. **Using const**:
If you declared the variable a using const earlier in your code, you cannot reassign a new value to it. const creates constants, which are meant to hold values that should not be changed.

```js
const a = 2; 
   a = 5; // This will cause the error
```

Fix:
If you need to change the value, use let instead of const:

```js
let a = 2;
     a = 5; // This is now allowed
```

If the value should truly remain constant, don't try to reassign it.

2. **Redeclaring with let in the same block**:
If you've used let to declare a within a block (like an if statement or a loop), and then try to redeclare it with let again within the same block, you'll get this error.

```js
if (true) {
       let a = 2;
       let a = 5; // Error: Cannot redeclare 'a' in the same block
   }
```

Fix:
Simply assign the new value without using let again:

```js
if (true) {
         let a = 2;
         a = 5; // This is now correct
     }
```

3. **Global Scope and Strict Mode**:
If you're not explicitly declaring a with var, let, or const, and you're in strict mode (using "use strict"), the browser will treat it as an attempt to assign to an undeclared variable, which is also not allowed.

```js
"use strict";
   a = 2; // Error in strict mode
```

Fix:
Always declare your variables using var, let, or const to avoid this issue:

```js
"use strict";
     let a = 2; // Now it's correct
```

To help me give you a more specific solution, please provide the following:
The surrounding code: Show me the code where you declare and try to modify the variable a.
Whether you're using strict mode: Let me know if you have "use strict" at the top of your JavaScript file.
Once you provide this information, I can give you a tailored solution to fix the error in your web app!

## Conclusion

This is a pretty cool integration of Gemini introduced by Google within Chrome DevTools to help us debug right away.

Hope the article helps :) 

Happy coding! 🚀

**Follow me for more such content**:

{% cta https://www.youtube.com/@ShameelUddin123 %} 🚀 Follow on YouTube {% endcta %}

{% cta https://www.linkedin.com/in/shameeluddin/ %} 🚀 Follow on LinkedIn {% endcta %}

{% cta https://github.com/Shameel123 %} 🚀 Follow on GitHub {% endcta %}