<?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: Arif Rawi</title>
    <description>The latest articles on DEV Community by Arif Rawi (@muhdarifrawi).</description>
    <link>https://dev.to/muhdarifrawi</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%2F717539%2Fceddfca7-8a48-4acc-b647-2a18c160402b.jpeg</url>
      <title>DEV Community: Arif Rawi</title>
      <link>https://dev.to/muhdarifrawi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhdarifrawi"/>
    <language>en</language>
    <item>
      <title>Env variables for browser JavaScript</title>
      <dc:creator>Arif Rawi</dc:creator>
      <pubDate>Wed, 16 Nov 2022 06:51:36 +0000</pubDate>
      <link>https://dev.to/muhdarifrawi/env-variables-for-browser-javascript-228b</link>
      <guid>https://dev.to/muhdarifrawi/env-variables-for-browser-javascript-228b</guid>
      <description>&lt;p&gt;As a developer fresh out of training, I realised how there were many other things I do not know how to do. One of it was how to create &lt;code&gt;env&lt;/code&gt; variables for a static webpage. &lt;/p&gt;

&lt;p&gt;Not everyone needs a Node environment or some other fancy stuff while creating their webpage. So in this small post, I'll share a quick solution if you intend to hide variables as you build your webpage. Do take note that this is meant for &lt;u&gt;development phase only&lt;/u&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are &lt;code&gt;env&lt;/code&gt; variables
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;env&lt;/code&gt; variables are short for environment variables. Lifting up from Wikipedia, an environment variable is "a dynamic-named value that can affect the way running processes will behave on a computer". Dynamic-named simply means that the name resolution will be done at runtime. &lt;/p&gt;

&lt;p&gt;Leaving those complicated definition aside, &lt;code&gt;env&lt;/code&gt; variables are widely used to store secrets and other variables you would like to keep away from prying eyes. An example of an &lt;code&gt;env&lt;/code&gt; would look something like this: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

secret_key=123456


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It's nothing new really. You just assign the value into the variable like always. The only difference is that this variable would be stored into a &lt;code&gt;.env&lt;/code&gt; file. This &lt;code&gt;.env&lt;/code&gt; file would also be listed in the &lt;code&gt;.gitignore&lt;/code&gt; file so as not to be pushed into your preferred Version Control System hosting service (such as GitHub).&lt;/p&gt;

&lt;h2&gt;
  
  
  So what's the problem?
&lt;/h2&gt;

&lt;p&gt;The problem lies in creating this &lt;code&gt;env&lt;/code&gt; variable for a static webpage. Which is, HTML5 with CSS and browser JavaScript. If you're using Python or Node JS, this could be resolved by simply installing the necessary packages.&lt;/p&gt;

&lt;p&gt;For browser JavaScript however, I have yet to see a simple solution for this. Hence why I am proposing this solution. &lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;The solution is simple. &lt;strong&gt;Create a separate JS file with functions storing the values&lt;/strong&gt;. Let's say that you want to store a secret phrase. This is how you would have to do it: &lt;/p&gt;
index.html



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"ie=edge"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Static Template&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;
      Here's what I fetched:
    &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insert-phrase"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"env.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
script.js



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;getPhrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretPhrase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showPhrase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#insert-phrase&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getPhrase&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;showPhrase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
env.js



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;secretPhrase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is my phrase.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
.gitignore



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

env.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/javascript-enc-16-nov-2022-tyv7si?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit JavaScript Enc - 16 Nov 2022"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;script.js&lt;/code&gt; is the main JS file. &lt;code&gt;env.js&lt;/code&gt; is where you store your &lt;code&gt;env&lt;/code&gt; variable. Here are some important things to take note.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In your HTML file, your &lt;u&gt;&lt;b&gt;&lt;code&gt;env&lt;/code&gt; file should come first&lt;/b&gt;&lt;/u&gt; before your other JS files. This is because the other files would be dependent on the &lt;code&gt;env&lt;/code&gt; file. &lt;/li&gt;
&lt;li&gt;You would need to &lt;u&gt;&lt;b&gt;declare a Global variable within the JS file&lt;/b&gt;&lt;/u&gt;, fetching the &lt;code&gt;env&lt;/code&gt; function. You cannot directly input the &lt;code&gt;env&lt;/code&gt; function, into another function that's residing in another JS file. &lt;/li&gt;
&lt;li&gt;
&lt;u&gt;&lt;b&gt;Write your &lt;code&gt;env.js&lt;/code&gt; into the &lt;code&gt;.gitignore&lt;/code&gt;&lt;/b&gt;&lt;/u&gt; or your secrets would be pushed out to your repository. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Last words
&lt;/h2&gt;

&lt;p&gt;One of my worry in creating this solution is what happens when you deploy your code out. Usually, sites would have settings where you can place your &lt;code&gt;env&lt;/code&gt; variables. Do take note that you may need to change parts of your code when you do so.&lt;/p&gt;

&lt;p&gt;I hope that this would help you in some way. It's not an elegant solution but it does what it's supposed to do. If you do have a simple alternative solution or feedback on why this might not be a good solution, don't hesitate to share!&lt;/p&gt;

&lt;p&gt;Thanks for reading ☺.&lt;/p&gt;

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