How to execute JavaScript in Chrome? In the Chrome browser, you can open the developer tools by pressing the F12 button or right-clicking the page and selecting "Inspect" .
You can also select "More Tools" = "Developer Tools" in the menu bar in the upper right corner to open:
1. Debugging JavaScript code in the Console window
After opening the developer tools, we can debug JavaScript code in the Console window, as shown below:
In the picture 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 elsewhere to execute it. For example, copy the following code to the Console window and press Enter to execute:
console.log("runoob-1")
console.log("runoob-2")
To clear the Console window to its contents, press the following button:
2. Chrome snippets
We can also create a script to execute in the Chrome browser. Click the Sources panel in the developer tools, select the Snippets tab, right-click the mouse in the navigator, and then select Create new snippet to create a new script file:
If you don't see Snippets, you can click on the panel to >> to see them.
After clicking Create new snippet, a file will be automatically created. You only need to enter the following code in the right window and 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: