<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Shelkovskiy on Medium]]></title>
        <description><![CDATA[Stories by Alex Shelkovskiy on Medium]]></description>
        <link>https://medium.com/@alexshelkov?source=rss-47ec68006883------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TiiGfxKM7ZZQ-ZDU.jpg</url>
            <title>Stories by Alex Shelkovskiy on Medium</title>
            <link>https://medium.com/@alexshelkov?source=rss-47ec68006883------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:00:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexshelkov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Learn JavaScript Scopes with Examples]]></title>
            <link>https://javascript.plainenglish.io/scopes-in-javascript-ebf776d42430?source=rss-47ec68006883------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebf776d42430</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Shelkovskiy]]></dc:creator>
            <pubDate>Thu, 03 Feb 2022 16:15:59 GMT</pubDate>
            <atom:updated>2022-02-22T06:49:28.448Z</atom:updated>
            <content:encoded><![CDATA[<h4>Understanding one of the most confusing parts of JavaScript with practical examples.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3yRTxnPj3kLtG7FNgGkuw.png" /></figure><p>Let’s start with simple tasks. Look at the code below and tell me what we would see in the console:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af62ce06c9d44db457f35892c47d62ab/href">https://medium.com/media/af62ce06c9d44db457f35892c47d62ab/href</a></iframe><p>The next one is almost the same, and the only thing we need is to change <strong><em>var</em></strong> to <strong><em>let</em></strong>. What do you think we will see now?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c24e98faae2f2e860b461aac25a4a6aa/href">https://medium.com/media/c24e98faae2f2e860b461aac25a4a6aa/href</a></iframe><p>If you think that in the first case the output will be:</p><pre><em>(wait 0sec) 3 (wait 1sec) 3 (wait 1sec ) 3</em></pre><p>And in second:</p><pre><em>(wait 0sec) 0 (wait 1sec) 1 (wait 1sec ) 2</em></pre><p>Then you are correct. But why? We changed only one word but got a different result. That is because of JavaScript scopes, which is one of the trickiest parts of the language. If you didn’t understand the examples above don’t worry, almost nobody does :)</p><h3>What is a scope?</h3><p>The scope is the place where you can <strong>access</strong> the variable (or function) in all other places the variable is not defined. And the tricky part is that JavaScript has <strong>two types</strong> of scopes: <strong>functional</strong> and <strong>block</strong>.</p><p>Functional scope is created when you define your variable (or function) with <strong>var</strong> or <strong>function</strong> keyword. Block scope will be created for a variable defined with <strong>let</strong> or <strong>const</strong> keyword.</p><p>One important thing to consider when dealing with a functional scope is variable hoisting. It means that the declaration of the variable will be put on top of the <strong>nearest</strong> function.</p><p>Let’s see how functional scope and hoisting works on example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6871f96f07cbe1e5e5bfe1af5ccc2ff/href">https://medium.com/media/e6871f96f07cbe1e5e5bfe1af5ccc2ff/href</a></iframe><p>We can rewrite the code above a bit to better understand how hoisting works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea2028478b5587968705d04fd7992220/href">https://medium.com/media/ea2028478b5587968705d04fd7992220/href</a></iframe><h3>Block scope</h3><p>So functions define the functional scope. And not surprisingly, code blocks (<strong><em>{}</em></strong>) are the definition of block scopes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f9c366f7c295a5b11ffe483f0220f4a/href">https://medium.com/media/2f9c366f7c295a5b11ffe483f0220f4a/href</a></iframe><h3>Back to where we started</h3><p>As you may already understand we use <strong>different types</strong> of scopes in our first examples. And that is why we got two absolutely different results. But to make it obvious let’s rewrite them to see it.</p><p>First one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67dba3503e7aad6e1b27855f7d4608b2/href">https://medium.com/media/67dba3503e7aad6e1b27855f7d4608b2/href</a></iframe><p>Or we can rewrite the same code using while loop (it will help us with the second example):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05fa8907665f134917acbba086108557/href">https://medium.com/media/05fa8907665f134917acbba086108557/href</a></iframe><p>Here is how the code above works step by step (it doesn’t matter what type of<br>loop used same logic work on <em>for</em> and <em>while</em>):</p><ol><li>on first iteration <strong>i = 0</strong>, so timeout delay is i*1000 = 0</li><li>setTimeout function delay the execution of timeout callback for 0 seconds</li><li>so the timeout callback does not run immediately, but only in the <strong>next stage of event loop</strong></li><li>cycle continues to run and now <strong>i = 1</strong>, so delay now is 1 sec, then <strong>i = 2</strong></li><li>and on the last step <strong>i = 3</strong> (but cycle not runs, because we have a condition i &lt; 3)</li><li>during our cycle we register 3 timeout callbacks with 0, 1, 2 seconds delay</li><li>our cycle ended, and even loop continue, now the timeout callback code start to run</li><li>so now, as at this moment, <strong>i = 3</strong>, and all 3 callbacks will print <strong>3</strong> as an output.</li></ol><p>When we declared our variable with <strong><em>var</em></strong> keyword we make it shared across all timeout callbacks, and as these callbacks run only <strong>after</strong> loop cycle ended they will see the same value.</p><p>To highlight the block scope in the second example, I am using a while loop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a00e684e4a0b23aadbf9753edad0ac57/href">https://medium.com/media/a00e684e4a0b23aadbf9753edad0ac57/href</a></iframe><p>The code works the same as in the first example and the only difference is that <strong>i</strong> is not shared across timeout callbacks, so each callback sees its own value.</p><p>These two simple examples show us the hidden complexity of JavaScript language, and now we understand how they work.</p><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Get exclusive access to writing opportunities and advice in our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>community Discord</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebf776d42430" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/scopes-in-javascript-ebf776d42430">Learn JavaScript Scopes with Examples</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monorepo’s with Typescript]]></title>
            <link>https://medium.com/poweredlocal/monorepos-with-typescript-8ee490235deb?source=rss-47ec68006883------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ee490235deb</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Alex Shelkovskiy]]></dc:creator>
            <pubDate>Mon, 06 Jul 2020 04:36:25 GMT</pubDate>
            <atom:updated>2020-07-07T02:12:48.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pJQECPTn7gez4Thc6sh0YQ.png" /></figure><p>These days, Monorepo’s and Typescript are very popular, but configuring the development environment to work with both is still a fairly complex task.</p><p>This post provides a short tutorial on how to configure the development environment, what things you might need and why.</p><p>If you don’t want to read it all and just want to see the results, you can clone this repo: <a href="https://github.com/PoweredLocal/typescript-starter">https://github.com/PoweredLocal/typescript-starter</a> and get started right away.</p><p>If you are still here let’s discuss what pieces you will need to create an almost perfect Typescript Monorepo setup.</p><h3>Intro to Lerna</h3><p>We need to start with <a href="https://github.com/lerna/lerna">Lerna</a> — a tool for managing JavaScript projects with multiple packages. It will link our packages together so we can use any package in the project as regular NPM dependency.</p><p>Run npm install -g lerna which will install Lerna globally, so it will be easy to run it later.</p><p>To continue, we need a configuration for Lerna. All we need to get started, is to tell Lerna where to search for our packages like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15601a977c83444bcb78729a6c865863/href">https://medium.com/media/15601a977c83444bcb78729a6c865863/href</a></iframe><p>Next, let’s create our first package in libs/ts-config, nothing special here, just create package.json as with any Node project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68ea8c975fbf6b248af73b71edff2bf4/href">https://medium.com/media/68ea8c975fbf6b248af73b71edff2bf4/href</a></iframe><blockquote>You may choose any <a href="https://docs.npmjs.com/creating-and-publishing-an-org-scoped-package">org name</a> you want or just skip it. In our example we use@poweredlocal. It is mostly required when you want to publish your packages to NPM</blockquote><p>Ok, next let’s try to use it in another project with Lerna. To do so just add it as a dependency:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d09b9a71f3a26d7c0f9082379697260/href">https://medium.com/media/2d09b9a71f3a26d7c0f9082379697260/href</a></iframe><p>Here is the Lerna magic comes into play. Run lerna bootstrap and these command link dependencies together. In your@services/test/index.js you may use require(&#39;@poweredlocal/ts-config&#39;).</p><p>Besides linking packages, Lerna may do a lot of other stuff. For us, the next important thing is running commands inside packages with lerna run &lt;command&gt;.</p><p>Now your project structure should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9065094e06dfe8dd1096d56a8c37ef45/href">https://medium.com/media/9065094e06dfe8dd1096d56a8c37ef45/href</a></iframe><p>We are ready to add Typescript.</p><h3>Typescript config</h3><p>Monorepo structure allows us to place some dependence into the root project, and we can follow this idea with all the things that we require for development, including Typescript itself.</p><p>In your root folder run npm intall -D typescript, and inside libs/ts-config we will keep all our Typescript settings which we will use in other projects. This will allow us to easily change Typescript config in one place without jumping between projects.</p><p>Here is the config itself libs/ts-config/tsconfig.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69450ec35d447c123e8288cfff0aad18/href">https://medium.com/media/69450ec35d447c123e8288cfff0aad18/href</a></iframe><p>Also, you need to create libs/ts-config/package.json (just copy it from above).</p><p>The package structure for Typescript will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daf0029573a1726066c666d79512e0ab/href">https://medium.com/media/daf0029573a1726066c666d79512e0ab/href</a></iframe><p>Compiled files will be stored in the dist folder. Next, let&#39;s create a library based on the config inside libs/lib-a folder.</p><p>Here is the complete libs/lib-a/package.json file which we need:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6db1ab3e235d8626ee1dd3ae95fda31e/href">https://medium.com/media/6db1ab3e235d8626ee1dd3ae95fda31e/href</a></iframe><p>Ignore scripts section for now and concentrate on dependencies. We added our config as dev dependency and Typescript itself as a peer dependency. Run lerna bootstrap and then create libs/lib-a/tsconfig.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef77487dccd23972b545509a463e8317/href">https://medium.com/media/ef77487dccd23972b545509a463e8317/href</a></iframe><p>As you can see we mostly extend from the basic config, but we have to specify rootDir, outDir and exclude dist. This is because of the way Typescript resolve this dirs relative to tsconfig.json file.</p><p>Also let’s add some code for testing libs/lib-a/src/index.ts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca2bfe864978b5b3c928f191bb91a9be/href">https://medium.com/media/ca2bfe864978b5b3c928f191bb91a9be/href</a></iframe><p>We are ready to use our lib-a in other packages, but first we need to build the project. To do this we can use npm run build or npm run build-dep. The former uses Lerna to resolve dependencies. It will build all project dependencies first and then the package itself. It is a convenient way to build the package because you don&#39;t have to manually rebuild all dependencies.</p><p>To reuse our lib in the services/test/package.json, add a new dependency to it @poweredlocal/lib-a (also don&#39;t forget to set up Typescript same way as we did for lib-a) and run lerna bootsrap.</p><p>After everything is done, you can require lib-a in test package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64c36a838debe8f5aaf7b6488d6d6929/href">https://medium.com/media/64c36a838debe8f5aaf7b6488d6d6929/href</a></iframe><p>Inside services/test run npm run build-dep &amp;&amp; node dist/index.js and you should finally see Hello world.</p><h3>Development tools</h3><p>Monorepo structure allows us to add ESLint, prettier, commit hooks, and even Jest only at the root of the project. The most complex thing to add is ESLint, because we have to configure it to check Typescript packages.</p><p>Here is how it may look:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/059ee16dff18b1a1626832e4c782b963/href">https://medium.com/media/059ee16dff18b1a1626832e4c782b963/href</a></iframe><p>We also need to install required dependencies in the root project:</p><pre>npm install -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-airbnb eslint-config-airbnb-typescript eslint-config-prettier eslint-plugin-prettier prettier</pre><p>Now we can check all packages in our project like this: lerna run check &amp;&amp; eslint . --ext .js,.ts.</p><h3>Check out our full-example</h3><p>In the <a href="https://github.com/PoweredLocal/typescript-starter">https://github.com/PoweredLocal/typescript-starter</a> repo you will see everything we set up above and few more things such as Jest and commit hooks. Just clone it run npm install &amp;&amp; lerna bootstrap and you are ready to go!</p><p>Thanks for reading and if you found this article helpful or interesting, hold down that clap button so others may find this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*MpQGrJRPkianLIYI8cymXg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ee490235deb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/poweredlocal/monorepos-with-typescript-8ee490235deb">Monorepo’s with Typescript</a> was originally published in <a href="https://medium.com/poweredlocal">WHAT THE FI. BY POWEREDLOCAL</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>