Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Executing JavaScript in the Chrome Browser

Executing JavaScript in the Chrome Browser

This chapter introduces how to run and debug JavaScript code in the Chrome browser.

Chrome is a free web browser developed by Google, and it is very convenient for front-end development (especially for debugging code).

Chrome official website:https://www.google.com/intl/zh-CN/chrome/。

In the Chrome browser, we can open DevTools by pressing F12 or right-clicking the page and selecting **"Inspect"**.

You can also select from the menu bar in the upper right corner "More tools" => "Developer tools" to open it:

1. Debugging JavaScript Code in the Console Window

After opening DevTools, we can debug JavaScript code in the Console window, as shown below:

In the figure above, we enter the code we want to execute after the > symbol, console.log("runoob"), and press Enter to execute it.

We can also copy a piece of code from elsewhere to execute, for example copy the following code into the Console window and press Enter to execute:

console.log("runoob-1")

console.log("runoob-2")

To clear the contents of the Console window, press the following button:


2. Chrome snippets Small Scripts

We can also create a script in the Chrome browser to execute. In DevTools, click the Sources panel, select the Snippets tab, right-click in the navigator, and then select Create new snippet to create a new script file:

If you do not see Snippets, you can click the >> on the panel below to see it.

After clicking Create new snippet, a file is created automatically. You only need to enter the following code in the right-hand window, then press Command+S (Mac) or Ctrl+S (Windows and Linux) to save the changes.



console.log("runoob-1")

console.log("runoob-2")

After saving, right-click the file name and select "Run" to execute the code: