<?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: HuanYang</title>
    <description>The latest articles on DEV Community by HuanYang (@baotlake).</description>
    <link>https://dev.to/baotlake</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%2F1459694%2F3ac560e7-b2a0-4b3e-8dbb-a09b8490e68b.png</url>
      <title>DEV Community: HuanYang</title>
      <link>https://dev.to/baotlake</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baotlake"/>
    <language>en</language>
    <item>
      <title>JavaScript Notebook First Edition（Node.js）</title>
      <dc:creator>HuanYang</dc:creator>
      <pubDate>Sun, 08 Sep 2024 02:49:30 +0000</pubDate>
      <link>https://dev.to/baotlake/javascript-notebook-first-editionnodejs-4ng0</link>
      <guid>https://dev.to/baotlake/javascript-notebook-first-editionnodejs-4ng0</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkka5g55xvrp6quzfiw2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkka5g55xvrp6quzfiw2.png" alt="Image description" width="800" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When talking about Notebooks, Python users often think of Jupyter Notebook. Jupyter Notebook is a widely-used interactive computing environment with broad applications in data analysis, visualization, and machine learning. After using Python Notebooks, I became interested in how to run JavaScript in a Notebook. In the JavaScript Notebook series, I'll be sharing some of my exploration experiences in this area.&lt;/p&gt;

&lt;p&gt;An "interactive computing environment" refers to how it runs code. It's neither purely a command-line REPL nor simply executing a .js file. Notebooks combine the advantages of both the command-line and file-based approaches by using cells. (REPL: Read-Eval-Print Loop, such as running the &lt;code&gt;node&lt;/code&gt; or &lt;code&gt;python&lt;/code&gt; commands in the terminal.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F268lv0fs44tteue2ezoa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F268lv0fs44tteue2ezoa.png" title="Visual Studio Code — Jupyter Notebook" alt="Image description" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a Notebook, code is written in individual cells, each functioning as an independent code block. You can run each cell separately and immediately see the results. Top-level variables defined in one cell can be accessed in other cells. When writing code in a cell, you benefit from IDE-like features such as autocomplete and syntax highlighting. By pressing Shift+Enter or clicking the run button, the code in the current cell is executed. If the output isn't correct, you can easily modify the code and rerun the cell.&lt;/p&gt;

&lt;p&gt;Now, back to the main topic: how can we run JavaScript in a Notebook? Jupyter Notebooks support various magic commands, and with the &lt;code&gt;%%script node&lt;/code&gt; magic command, we can execute JavaScript directly within the Notebook. (This requires Node.js to be installed; if it's already installed, you can run it right away.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw8imuuyz0i7c2ffn0c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw8imuuyz0i7c2ffn0c9.png" alt="Image description" width="800" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use ESM syntax in a cell, you can add the &lt;code&gt;-experimental-default-type module&lt;/code&gt; flag, which is supported in Node.js version 21 and above.&lt;/p&gt;

&lt;p&gt;While this method is relatively simple and practical, it has the following limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Syntax highlighting and autocomplete are not fully supported.&lt;/li&gt;
&lt;li&gt;Variables cannot be referenced across cells.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the upcoming JavaScript Notebook series, I will introduce other methods that offer a better experience. If you're interested in JavaScript Notebooks, stay tuned for more insights.&lt;/p&gt;




&lt;p&gt;Magic commands that start with &lt;code&gt;%%&lt;/code&gt; are called &lt;strong&gt;Cell Magic&lt;/strong&gt; commands, and they apply to the entire cell. The &lt;code&gt;%%script node&lt;/code&gt; command means that the code in the cell will be executed using the Node.js interpreter. &lt;/p&gt;

&lt;p&gt;Jupyter Notebook also supports the &lt;code&gt;%%js&lt;/code&gt; and &lt;code&gt;%%javascript&lt;/code&gt; magic commands. Unlike &lt;code&gt;%%script node&lt;/code&gt;, these two commands run JavaScript code on the &lt;strong&gt;front end&lt;/strong&gt; of the Notebook, in a web page environment, meaning that &lt;code&gt;console.log&lt;/code&gt; output won’t appear below the cell. Similar to &lt;code&gt;%%js&lt;/code&gt;, there are also &lt;code&gt;%%html&lt;/code&gt; and &lt;code&gt;%%svg&lt;/code&gt; commands, which render their respective outputs directly below the cell.&lt;/p&gt;

&lt;p&gt;A Notebook is essentially a JSON file with a &lt;code&gt;.ipynb&lt;/code&gt; extension, and it requires a notebook-compatible editor to open. VSCode supports Notebooks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>vscode</category>
      <category>node</category>
    </item>
  </channel>
</rss>
