You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: microsoft-edge/devtools-guide-chromium/console/console-javascript.md
+68-4Lines changed: 68 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ ms.author: msedgedevrel
6
6
ms.topic: conceptual
7
7
ms.service: microsoft-edge
8
8
ms.subservice: devtools
9
-
ms.date: 07/12/2023
9
+
ms.date: 06/06/2025
10
10
---
11
11
# Run JavaScript in the Console
12
12
13
-
You can enter any JavaScript expression, statement, or code snippet in the **Console**, and it runs immediately and interactively as you type. This is possible because the **Console** tool in DevTools is a [REPL](https://wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) environment. _REPL_ stands for Read, Evaluate, Print, and Loop.
13
+
You can enter any JavaScript expression, statement, or code snippet in the **Console**, and it runs immediately and interactively as you type. This is possible because the **Console** tool in DevTools is a [Read–eval–print loop (REPL)](https://wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) environment.
14
14
15
15
The **Console**:
16
16
1. Reads the JavaScript that you type into it.
@@ -65,15 +65,17 @@ As with many other command-line environments, a history of the commands that you
65
65
66
66
Similarly, autocompletion keeps a history of the commands you previously typed. You can type the first few letters of earlier commands, and your previous choices appear in a text box.
67
67
68
-
Also, the **Console** also offers quite a few [utility methods](utilities.md) that make your life easier. For example, `$_` always contains the result of the last expression you ran in the **Console**.
68
+
Also, the **Console** also offers quite a few utility methods that make your life easier. For example, `$_` always contains the result of the last expression you ran in the **Console**. See [Console tool utility functions and selectors](./utilities.md).
69
69
70
70

By default, the **Console** only gives you one line to write your JavaScript expression. You code runs when you press **Enter**. The one line limitation may frustrate you. To work around the 1-line limitation, press **Shift+Enter** instead of **Enter**. In the following example, the value displayed is the result of all the lines (statements) run in order:
76
+
By default, the **Console** only gives you one line to write your JavaScript expression. You code runs when you press **Enter**. To work around the 1-line limitation, press **Shift+Enter** instead of **Enter**.
77
+
78
+
In the following example, the value displayed is the result of all the lines (statements) run in order:
77
79
78
80

79
81
@@ -82,6 +84,50 @@ If you start a multi-line statement in the **Console**, the code block is automa
82
84

When you first try to paste content into the **Console** tool, instead of pasting, a message is displayed: "Warning: Don't paste code into the DevTools Console that you don't understand or haven't reviewed yourself. This could allow attackers to steal your identity or take control of your computer. Please type 'allow pasting' below and press Enter to allow pasting."
91
+
92
+

93
+
94
+
This warning helps prevent self cross-site scripting attacks (self-XSS) on end-users. To paste code, first type **allow pasting** in the **Console**, and then press **Enter**. Then paste the content. Or, start Edge with the flag below.
95
+
96
+
Pasting into the **Sources** tool's snippet editor is similar; see [Allow pasting into the Snippet editor](../javascript/snippets.md#allow-pasting-into-the-snippet-editor) in _Run snippets of JavaScript on any webpage_.
97
+
98
+
99
+
<!-- ------------------------------ -->
100
+
#### Disable self-XSS warnings by starting Edge with a command-line flag
101
+
102
+
To prevent the above warnings and immediately allow pasting into the **Console** tool and the **Sources** tool's snippet editor, such as for automated testing, start Microsoft Edge from the command line, using the following flag: `--unsafely-disable-devtools-self-xss-warnings`. The flag applies to a single session of Microsoft Edge.
@@ -120,3 +166,21 @@ To get the last 50 issues that were filed on the [Microsoft Edge Developer Tools
120
166
```
121
167
122
168
The **Console** is a great way to practice JavaScript and to do some quick calculations. The real power is the fact that you have access to the [window](https://developer.mozilla.org/docs/Web/API/Window) object. See [Interact with the DOM using the Console](console-dom-interaction.md).
If you're entering the same code into the **Console** tool repeatedly, consider saving the code as a snippet instead, and then running the snippet. Snippets are scripts that you author in the **Sources** tool. Snippets have access to the JavaScript context of the webpage, and you can run snippets on any webpage. Snippets can be used to alter a webpage, such as to change its content or appearance, or to extract data.
27
28
@@ -50,13 +51,17 @@ The code changes the background color of the webpage to dimgrey, adds a new line
50
51
51
52
When you run a snippet on a webpage, the snippet's source code is added to the current webpage. For more information about changing the existing code of a webpage instead of adding new code, see [Override webpage resources with local copies (Overrides tab)](./overrides.md).
52
53
54
+
55
+
<!-- ------------------------------ -->
53
56
#### Include all your code in one file
57
+
<!-- not upstream -->
54
58
55
59
The security settings of most webpages block from loading other scripts in snippets. For this reason, you must include all your code in one file.
<!-- Open the Snippets pane https://developer.chrome.com/docs/devtools/javascript/snippets/#open -->
60
65
61
66
The **Snippets** tab is grouped with the **Page** tab in the **Navigator** pane, on the left of the **Sources** tool.
62
67
@@ -71,7 +76,9 @@ To open the **Snippets** tab:
71
76
1. In the **Navigator** pane (on the left), select the **Snippets** tab. To access the **Snippets** option, you might need to click the **More tabs** () button.
72
77
73
78
79
+
<!-- ------------------------------ -->
74
80
#### Open the Snippets tab from the Command Menu
81
+
<!-- no separate heading upstream -->
75
82
76
83
You can also open the **Snippets** tab by using the **Command Menu**:
77
84
@@ -85,7 +92,17 @@ You can also open the **Snippets** tab by using the **Command Menu**:
You can create a snippet from within the **Snippets** tool, or by running the **Create new snippet** command from the **Command Menu** anywhere in DevTools.
99
+
100
+
The **Snippets** pane sorts your snippets in alphabetical order.
101
+
102
+
103
+
<!-- ------------------------------ -->
104
+
#### Create a snippet in the Sources tool
105
+
<!-- Create a snippet in the Sources panel https://developer.chrome.com/docs/devtools/javascript/snippets/#create-sources -->
89
106
90
107
To create a new snippet from the **Snippets** tab:
91
108
@@ -97,7 +114,10 @@ To create a new snippet from the **Snippets** tab:
97
114
98
115

99
116
100
-
#### Create a new snippet from the Command Menu
117
+
118
+
<!-- ------------------------------ -->
119
+
#### Create a snippet from the Command Menu
120
+
<!-- Create a snippet from the Command Menu https://developer.chrome.com/docs/devtools/javascript/snippets/#create-command-menu -->
101
121
102
122
1. Focus your cursor somewhere in DevTools.
103
123
@@ -112,6 +132,7 @@ To rename your new snippet, see [Rename a snippet](#rename-a-snippet), below.
When you first try to paste content into the **Sources** tool > **Snippets** tab > snippet editor, instead of pasting, a **Do you trust this code?** dialog is displayed, with the message: "Don't paste code you do not understand or have not reviewed yourself into DevTools. This could allow attackers to steal your identity or take control of your computer. Please type 'allow pasting' below to allow pasting."
157
+
158
+

159
+
160
+
This warning helps prevent self cross-site scripting attacks (self-XSS) on end-users. To paste code, first type **allow pasting** in the dialog's text box, and then click the **Allow** button. Then paste the content. Or, start Edge with the flag below.
161
+
162
+
Pasting into the **Console** tool is similar; see [Allow pasting into the Console](../console/console-javascript.md#allow-pasting-into-the-console) in _Run JavaScript in the Console_.
163
+
164
+
165
+
<!-- ------------------------------ -->
166
+
#### Disable self-XSS warnings by starting Edge with a command-line flag
167
+
<!-- not upstream -->
168
+
169
+
To prevent the above warnings and immediately allow pasting into the **Console** tool and the **Sources** tool's snippet editor, such as for automated testing, start Microsoft Edge from the command line, using the following flag: `--unsafely-disable-devtools-self-xss-warnings`. The flag applies to a single session of Microsoft Edge.
0 commit comments