<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: NGS Harsha</title>
    <description>The latest articles on DEV Community by NGS Harsha (@ganeshh___).</description>
    <link>https://dev.to/ganeshh___</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F314500%2F79316559-1ce3-4823-8bcc-238dbaaf6c90.png</url>
      <title>DEV Community: NGS Harsha</title>
      <link>https://dev.to/ganeshh___</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ganeshh___"/>
    <language>en</language>
    <item>
      <title>JavaScript Snippets In The Browser</title>
      <dc:creator>NGS Harsha</dc:creator>
      <pubDate>Sat, 31 Jul 2021 09:15:48 +0000</pubDate>
      <link>https://dev.to/ganeshh___/javascript-snippets-in-the-browser-1gf1</link>
      <guid>https://dev.to/ganeshh___/javascript-snippets-in-the-browser-1gf1</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Sometimes, when building web applications, I want to test a piece of JavaScript code before actually writing it in the application and without switching between the editor and the browser. For example, checking the response of an API call using fetch or filtering out an array or converting the response JSON into the required format.&lt;/p&gt;

&lt;p&gt;We can use the &lt;strong&gt;&lt;em&gt;console&lt;/em&gt;&lt;/strong&gt; tab in the browser's dev tools to write JavaScript, just type a line and hit enter. If we want to write multi-line expressions, we can use shift + enter to break into a new line, and hit enter at the end to execute all those lines. We don't have to use shift + enter always, sometimes the browser will automatically break into a new line after hitting enter if the expression is a multi-line expression (e.g., loops, function declarations etc.).&lt;/p&gt;

&lt;p&gt;But, when we want to view and edit a piece of code we executed before, we have to use the up arrow key ↑ (just like cycling through terminal history). When we do that, we can't expect the browser to break into a new line when you hit enter inside a loop or a function while editing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Trick
&lt;/h2&gt;

&lt;p&gt;Chrome and Firefox have a feature (I don't know about Safari) where you can write JavaScript snippets just like you do in your text editor (with limited IntelliSense, and autocompletion of course).&lt;/p&gt;

&lt;h3&gt;
  
  
  For Chrome
&lt;/h3&gt;

&lt;p&gt;In Chrome, open dev tools by pressing F12 and navigate to the &lt;strong&gt;&lt;em&gt;Sources&lt;/em&gt;&lt;/strong&gt; tab. Under that tab, on the left side, you will see a tab called &lt;strong&gt;&lt;em&gt;Snippets&lt;/em&gt;&lt;/strong&gt;, click on that. If you don't see it, click on the double angled brackets button (circled in red below) to expand the menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8vre03rm9hhj6adeg0b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8vre03rm9hhj6adeg0b.jpg" alt="Chrome Dev Tools Snippets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, click on the &lt;strong&gt;&lt;em&gt;New snippet&lt;/em&gt;&lt;/strong&gt; button to create a new snippet. You can name it whatever you want or leave it as it is. The browser will open the new snippet on the right side. There you can write your JavaScript code and run it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fthr1x59p3pxn3qulzu5j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fthr1x59p3pxn3qulzu5j.jpg" alt="Chrome Dev Tools Snippets Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After writing the code, click on the run/play button on the bottom right corner (highlighted in green) or press ctrl + enter, to run it. You can format the code using the button on the bottom left corner (circled in red above). You can view the output in the &lt;strong&gt;&lt;em&gt;Console&lt;/em&gt;&lt;/strong&gt; tab.&lt;/p&gt;

&lt;h3&gt;
  
  
  For Firefox
&lt;/h3&gt;

&lt;p&gt;In Firefox, open dev tools by pressing F12 and navigate to the &lt;strong&gt;&lt;em&gt;Console&lt;/em&gt;&lt;/strong&gt; tab. on the top right corner of that tab, you will see a button (circled in red below). Clicking that will open a multi-line editor to the left. There you can write your JavaScript code and run it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31txjkmo7jqvjp2bbpyn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31txjkmo7jqvjp2bbpyn.jpg" alt="Firefox Dev Tools Snippets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After writing the code, click on the run button or hit ctrl + enter to run it. You can format the code using the button with curly brackets (circled in red below). The output will be displayed on the right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fibsj7bksuppec3qz9dt0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fibsj7bksuppec3qz9dt0.jpg" alt="Firefox Dev Tools Snippets Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;We can run the code multiple times in Chrome without any errors. But Firefox gives &lt;strong&gt;&lt;em&gt;redeclaration&lt;/em&gt;&lt;/strong&gt; error if we try to run the code multiple times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cjlq6l50xiyukke556w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cjlq6l50xiyukke556w.jpg" alt="Firefox Redeclaration Error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this error, wrap the code in a function and call it or write an IIFE (Immediately Invoked Function Expression)😉&lt;/p&gt;

&lt;h3&gt;
  
  
  Read Further
&lt;/h3&gt;

&lt;p&gt;Read more about Chrome's snippets feature &lt;a href="https://developer.chrome.com/docs/devtools/javascript/snippets/" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and about Firefox's multi-line editor mode &lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Web_Console/The_command_line_interpreter#multi-line_mode" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This article is available on my blog as well, check it out &lt;a href="https://ngsharsha.netlify.app/blog/javascript-snippets-in-the-browser/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
