<?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"><channel><title><![CDATA[Christian Holm • Data Analyst]]></title><description><![CDATA[Sharing my knowledge about data analytics and programming.]]></description><link>https://christianholm.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 14:01:58 GMT</lastBuildDate><atom:link href="https://christianholm.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Fundamentals of SQL]]></title><description><![CDATA[Introduction
I'm currently learning SQL from DataQuest and other resources. In this blog post, I'll write about what I've learned about the following essential clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. These clauses provide...]]></description><link>https://christianholm.dev/fundamentals-of-sql</link><guid isPermaLink="true">https://christianholm.dev/fundamentals-of-sql</guid><category><![CDATA[SQL]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Fri, 14 Apr 2023 07:48:55 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>I'm currently learning SQL from DataQuest and other resources. In this blog post, I'll write about what I've learned about the following essential clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. These clauses provide the foundation for constructing powerful and efficient queries. Later I'll write about more advanced techniques like joins, window functions and common table expressions.</p>
<h3 id="heading-select">SELECT</h3>
<p>The SELECT clause is the starting point of any SQL query. It allows you to choose the columns you want to retrieve from your database table. You can either specify each column individually or use an asterisk (*) to select all columns in the table. Selecting only the necessary columns can significantly improve query performance, especially when working with large datasets.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> first_name, last_name, email
  <span class="hljs-keyword">FROM</span> customers;
</code></pre>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> *
  <span class="hljs-keyword">FROM</span> customers;
</code></pre>
<h3 id="heading-from">FROM</h3>
<p>The FROM clause is used to specify the table or tables that a query will retrieve data from. It is an essential part of a SELECT statement, which is used to retrieve data from a database. The FROM clause is followed by the name of the table or tables that contain the data that you want to retrieve. In most cases, you will only need to specify a single table, but you can also retrieve data from multiple tables using JOIN clauses, but I won't go into detail about that here.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> first_name, last_name, address
  <span class="hljs-keyword">FROM</span> employees;
</code></pre>
<h3 id="heading-where">WHERE</h3>
<p>The WHERE clause enables you to filter data based on specific conditions. This is particularly useful when you want to analyze a subset of your data or retrieve records that meet specific criteria. Using the WHERE clause effectively can help reduce the amount of data you need to process and improve query performance.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> first_name, last_name, age
  <span class="hljs-keyword">FROM</span> customers
 <span class="hljs-keyword">WHERE</span> age &gt;= <span class="hljs-number">18</span>;
</code></pre>
<h3 id="heading-group-by">GROUP BY</h3>
<p>The GROUP BY clause allows you to group rows that have the same values in specified columns. This is especially useful when you want to perform aggregate functions like COUNT, SUM, AVG, MIN, or MAX on your data. GROUP BY is an essential tool for summarizing and analyzing large datasets.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> department_id, <span class="hljs-keyword">COUNT</span>(employee_id) <span class="hljs-keyword">as</span> num_employees
  <span class="hljs-keyword">FROM</span> employees
 <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> department_id;
</code></pre>
<h3 id="heading-having">HAVING</h3>
<p>The HAVING clause is used in combination with GROUP BY to filter the results of an aggregate function. While the WHERE clause filters rows before they are grouped, the HAVING clause filters groups after they have been aggregated. This enables you to focus on specific groups that meet your criteria.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> department_id, <span class="hljs-keyword">COUNT</span>(employee_id) <span class="hljs-keyword">as</span> num_employees
  <span class="hljs-keyword">FROM</span> employees
 <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> department_id
<span class="hljs-keyword">HAVING</span> num_employees &gt; <span class="hljs-number">10</span>;
</code></pre>
<h3 id="heading-order-by">ORDER BY</h3>
<p>The ORDER BY clause allows you to sort the result set based on one or more columns in ascending (ASC) or descending (DESC) order. This is particularly helpful when you want to display data in a specific order for reporting or further analysis. The default sorting is ASC, so you don't need to type it out if you want to sort from smallest to largest.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> first_name, last_name, salary
  <span class="hljs-keyword">FROM</span> employees
 <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> salary <span class="hljs-keyword">DESC</span>;
</code></pre>
<h3 id="heading-limit">LIMIT</h3>
<p>The LIMIT clause enables you to restrict the number of rows returned by a query. This can be useful when you want to view a sample of your data or display only the top results. Limiting the number of rows returned can also improve query performance.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> first_name, last_name, salary
  <span class="hljs-keyword">FROM</span> employees
 <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> salary <span class="hljs-keyword">DESC</span>
 <span class="hljs-keyword">LIMIT</span> <span class="hljs-number">10</span>;
</code></pre>
<h2 id="heading-order-of-clauses">Order of clauses</h2>
<p>There's a recommended order of clauses when you're writing SQL queries. The order is generally flexible and can vary depending on the requirements of the query. However, there are some best practices for organizing the clauses in a logical and readable manner. Usually, you'll want to order your query like this:</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> &gt; <span class="hljs-keyword">FROM</span> &gt; <span class="hljs-keyword">WHERE</span> &gt; <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> &gt; <span class="hljs-keyword">HAVING</span> &gt; <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> &gt; <span class="hljs-keyword">LIMIT</span>
</code></pre>
<p>However, the order of clauses is not the same as the order of execution.</p>
<h2 id="heading-order-of-execution">Order of execution</h2>
<p>The order of execution refers to the order in which the different parts of the query are processed by the database engine:</p>
<pre><code class="lang-sql">FROM &gt; WHERE &gt; GROUP BY &gt; HAVING &gt; <span class="hljs-keyword">SELECT</span> &gt; <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> &gt; <span class="hljs-keyword">LIMIT</span>
</code></pre>
<p>The database engine first evaluates the <code>FROM</code> clause to determine the tables to be used in the query, then applies any join conditions specified in the <code>JOIN</code> clauses, and so on.</p>
]]></content:encoded></item><item><title><![CDATA[Google Data Analytics - Foundations]]></title><description><![CDATA[Part 1 - Foundations
The first course of the Google Data Analytics Certificate focuses on introducing the process of data analytics, talking about what data is and how analyzing data can provide insights that can help businesses make better data-driv...]]></description><link>https://christianholm.dev/google-data-analytics-foundations</link><guid isPermaLink="true">https://christianholm.dev/google-data-analytics-foundations</guid><category><![CDATA[Data Science]]></category><category><![CDATA[Certification]]></category><category><![CDATA[Google]]></category><category><![CDATA[analytics]]></category><category><![CDATA[data analysis]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Mon, 13 Mar 2023 20:42:21 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-part-1-foundations">Part 1 - Foundations</h2>
<p>The first course of the Google Data Analytics Certificate focuses on introducing the process of data analytics, talking about what data is and how analyzing data can provide insights that can help businesses make better data-driven decisions.</p>
<h3 id="heading-data-science-analysis-analytics">Data science - analysis - analytics</h3>
<p>Data science is a way of creating new ways of modeling and understanding the unknown by using raw data. Data scientists use data to create new questions, while data analysts create insights from existing data to answer questions.</p>
<p>Data analytics is basically the science of data. It's a wide-ranging concept that includes everything from managing data to the methods and tools used by data scientists and other data workers.</p>
<h3 id="heading-the-role-of-data-in-decision-making">The role of data in decision-making</h3>
<p>You can use data to find facts that can help guide business strategy. A data analyst can get data, analyze it and use it to find patterns and relationships. To get the best results, it's important to include insights from people who are familiar with the business problem, ie. subject matter experts. These people can help find inconsistencies and guide you through gray areas, and validate the choices being made.</p>
<h3 id="heading-key-skills-for-a-data-analyst">Key skills for a data analyst</h3>
<p>A data analyst should possess analytical thinking skills, ie. solving problems using facts in an organized, step-by-step process. There are five key aspects to analytical thinking:</p>
<ol>
<li><p><strong>Visualization</strong> - the graphical representation of data</p>
</li>
<li><p><strong>Strategy</strong> - having a strategic mindset is key to staying focused</p>
</li>
<li><p><strong>Problem-orientation</strong> - data analysts use a problem-oriented approach to identify, describe, and solve problems</p>
</li>
<li><p><strong>Correlation</strong> - keeping in mind that correlation doesn't equal causation</p>
</li>
<li><p><strong>Big-picture and detail-oriented thinking</strong> - being able to see the big picture, as well as the small details</p>
</li>
</ol>
<h3 id="heading-core-analytical-skills">Core analytical skills</h3>
<p>Data analysts often ask what the root cause of a problem is. The "five why's" can benefit the process of finding the root cause. It's a very helpful tool in data analysis. You simply ask "why" five times to find the real reason.</p>
<p>Another good method to know is gap analysis. It's a method for evaluating how a process currently works, in order to get to where you want to be in the future. Then you can identify the gaps that exist between the current and future state.</p>
<h3 id="heading-data-life-cycle-stages">Data life cycle stages</h3>
<p>The data life cycle provides a common framework for how data is managed:</p>
<ol>
<li><p><strong>Plan -</strong> decide what kind of data is needed, how it will be managed, and who will be responsible for it</p>
</li>
<li><p><strong>Capture</strong> - collect or bring in data from a variety of different sources</p>
</li>
<li><p><strong>Manage</strong> - care for and maintain the data. This includes determining how and where it is stored and the tools used to do so</p>
</li>
<li><p><strong>Analyze -</strong> use the data to solve problems, make decisions, and support business goals</p>
</li>
<li><p><strong>Archive -</strong> keep relevant data stored for long-term and future reference</p>
</li>
<li><p><strong>Destroy -</strong> remove data from storage and delete any shared copies of the data</p>
</li>
</ol>
<p><em>Don't mix up the six stages of the data life cycle with the six phases of the data analysis life cycle. They are not the same thing.</em></p>
<h3 id="heading-the-six-steps-of-data-analysis">The six steps of data analysis</h3>
<p>The data analysis process consists of six steps:</p>
<ol>
<li><p><strong>Ask</strong> questions and define the problem</p>
</li>
<li><p><strong>Prepare</strong> data by collecting and storing the information</p>
</li>
<li><p><strong>Process</strong> data by cleaning and checking the information</p>
</li>
<li><p><strong>Analyze</strong> data to find trends, relationships, and patterns</p>
</li>
<li><p><strong>Share</strong> data with your audience</p>
</li>
<li><p><strong>Act</strong> on the data and use the results of the analysis</p>
</li>
</ol>
<h3 id="heading-ask">Ask</h3>
<p>In this phase you define the problem to be solved, and make sure you fully understand expectations from stakeholders. The "five why's" method can be very helpful in this phase.</p>
<h3 id="heading-prepare">Prepare</h3>
<p>In this phase, you collect and store data for the upcoming data analysis process.</p>
<h3 id="heading-process">Process</h3>
<p>Here you find and eliminate errors and inconsistencies in the data. That means cleaning data, perhaps transforming it into a more useful format, combining datasets to make information more complete, and removing any outliers that could skew the information.</p>
<h3 id="heading-analyze">Analyze</h3>
<p>In this phase, you use tools to organize and transform data in order to draw useful insights and drive informed decision-making. Data analysts use a lot of powerful tools in their work, eg. spreadsheets and SQL.</p>
<h3 id="heading-share">Share</h3>
<p>Here you'll share the results of your work so stakeholders can make effective data-driven decisions. In this phase, you'll use visualization tools to make complex concepts and facts easier to understand.</p>
<h3 id="heading-act">Act</h3>
<p>In the final phase, businesses will take all your insights and put them to work to solve the original business problem.</p>
<h3 id="heading-the-data-analysis-toolbox">The data analysis toolbox</h3>
<p>The most common tool used by data analysts include spreadsheets like Excel, structured query languages and databases, and visualization tools.</p>
<p>By putting the data in a spreadsheet you can see patterns, group and easily find the information you need. With SQL data analysts can access the data they need by making requests to a database. Finally, data analysts use visualization to represent information and better communicate their insights in a compelling way. Some data analysts also use programming languages such as Python or R for data analysis and visualization.</p>
<h3 id="heading-the-importance-of-fairness">The importance of fairness</h3>
<p>Data analysts need to ensure their analysis doesn't create or reinforce bias. And sometimes conclusions based on data can be both true and unfair, and as a data analyst it's your job to make sure your analysis is fair and factors in the social context that could create bias in your conclusions.</p>
]]></content:encoded></item><item><title><![CDATA[Closures]]></title><description><![CDATA[Closures in JavaScript
Closures can be a confusing subject. But before we get into exactly what closures are, we should refresh our knowledge of scope since it's linked to closures. 
Scope
Generally, scope works from inside to outside. So inside a fu...]]></description><link>https://christianholm.dev/closures</link><guid isPermaLink="true">https://christianholm.dev/closures</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[closure]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Thu, 23 Feb 2023 11:31:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645095736257/hlh8sdl1H.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-closures-in-javascript">Closures in JavaScript</h1>
<p>Closures can be a confusing subject. But before we get into exactly what closures are, we should refresh our knowledge of scope since it's linked to closures. </p>
<h2 id="heading-scope">Scope</h2>
<p>Generally, scope works from inside to outside. So inside a function the function has access to its own local variables, and all variables in outer scopes - ie. global objects. But it won’t have access to variables that reside inside other functions - unless it’s nested inside another function, then it has access to its parent’s scope (and its parents, etc.). Let’s look at an example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> globalVariable = <span class="hljs-string">"I'm global!"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> localVariable = <span class="hljs-string">"I'm local to myFunction!"</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">insideFunction</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> innerVariable = <span class="hljs-string">"I'm in insideFunction!"</span>;

    <span class="hljs-built_in">console</span>.log(innerVariable); <span class="hljs-comment">// "I'm in insideFunction!";</span>
    <span class="hljs-built_in">console</span>.log(localVariable); <span class="hljs-comment">// "I'm local to myFunction!";</span>
    <span class="hljs-built_in">console</span>.log(globalVariable); <span class="hljs-comment">// "I'm global!";</span>
  }
}
</code></pre>
<p>So insideFunction has access to the variables inside itself, and the parent myFunction, and finally the global scope (ie.<code>globalVariable</code>). But it doesn’t work from the outside in. Out in the global scope you can’t <code>console.log</code> neither <code>localVariable</code> (in myFunction) or <code>innerVariable</code> (in insideFunction):</p>
<pre><code class="lang-jsx"><span class="hljs-built_in">console</span>.log(innerVariable); 
<span class="hljs-comment">// "ReferenceError: innerVariable is not defined" since it's inside a function and not available in global scope</span>
</code></pre>
<h2 id="heading-closures">Closures</h2>
<p>Closure basically means that a function does not lose connection to the variables that existed when the function was created. Let’s look at an example of how this works:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">counterFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    count += <span class="hljs-number">1</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Count is now <span class="hljs-subst">${count}</span>`</span>);
  };
}

<span class="hljs-keyword">let</span> result = counterFunction();
result(); <span class="hljs-comment">// "Count is now 1"</span>
result(); <span class="hljs-comment">// "Count is now 2"</span>
result(); <span class="hljs-comment">// "Count is now 3"</span>
</code></pre>
<p>As you can see in the above example, the <code>result</code> function still has access to the <code>count</code> variable created inside <code>counterFunction</code>, even though that function has finished executing. It works because <code>counterFunction</code> returned a new function, which <em>closes over</em> its parent function and keeps access to its variables, even after the parent function is done executing. That’s how we’re able to use <code>result</code> to increase the <code>count</code> variable. </p>
<h2 id="heading-summary">Summary</h2>
<p>We have seen how scope works from inside to outside, and how a function closure preserves the outer scope inside its inner scope.</p>
]]></content:encoded></item><item><title><![CDATA[Object & Array Destructuring]]></title><description><![CDATA[Destructuring in JavaScript
ES6 introduced the new destructing assignment feature, which makes it easy to destructure objects and arrays into variables.
Object destructuring
Let's take object destructuring:
const user = {
  firstName: "Christian",
  ...]]></description><link>https://christianholm.dev/object-and-array-destructuring</link><guid isPermaLink="true">https://christianholm.dev/object-and-array-destructuring</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Fri, 17 Feb 2023 11:32:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1645042009014/uxoSt6B6c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-destructuring-in-javascript">Destructuring in JavaScript</h1>
<p>ES6 introduced the new destructing assignment feature, which makes it easy to destructure objects and arrays into variables.</p>
<h2 id="heading-object-destructuring">Object destructuring</h2>
<p>Let's take object destructuring:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> user = {
  <span class="hljs-attr">firstName</span>: <span class="hljs-string">"Christian"</span>,
  <span class="hljs-attr">lastName</span>: <span class="hljs-string">"Holm"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">41</span>
}
</code></pre>
<p>Before ES6 we would have to get the properties out of the object manually, by writing a new variable assignment for each property we wanted:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> firstName = user.firstName;  <span class="hljs-comment">// "Christian"</span>
<span class="hljs-keyword">var</span> lastName = user.lastName;  <span class="hljs-comment">// "Holm"</span>
<span class="hljs-keyword">var</span> age = user.age;  <span class="hljs-comment">// 41</span>
</code></pre>
<p>But with modern JavaScript we can use object destructuring:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { firstName, lastName, age } = user;

<span class="hljs-built_in">console</span>.log(firstName); <span class="hljs-comment">// "Christian",</span>
<span class="hljs-built_in">console</span>.log(lastName); <span class="hljs-comment">// "Holm",</span>
<span class="hljs-built_in">console</span>.log(age); <span class="hljs-comment">// 41</span>
</code></pre>
<p>The above code example creates the same three variables with data from the object.</p>
<p>If we only need firstname and lastname, we can specify just those two variables:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { firstName, lastName } = user;
</code></pre>
<h3 id="heading-alias">Alias</h3>
<p>We can also create variables with different names:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { <span class="hljs-attr">firstName</span>: name, <span class="hljs-attr">lastName</span>: last } = user;

<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// "Christian"</span>
<span class="hljs-built_in">console</span>.log(last); <span class="hljs-comment">// "Holm"</span>
</code></pre>
<h3 id="heading-default-value">Default value</h3>
<p>You can set a default value in case the property doesn't exist in the object:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { firstName, lastName, age, country = Denmark } = user;

<span class="hljs-built_in">console</span>.log(firstName); <span class="hljs-comment">// "Christian",</span>
<span class="hljs-built_in">console</span>.log(lastName); <span class="hljs-comment">// "Holm",</span>
<span class="hljs-built_in">console</span>.log(age); <span class="hljs-comment">// 41</span>
<span class="hljs-built_in">console</span>.log(country); <span class="hljs-comment">// Denmark</span>
</code></pre>
<h3 id="heading-nested-objects">Nested objects</h3>
<p>You can destructure the properties of a nested object into individual variables:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Object:</span>
<span class="hljs-keyword">const</span> admin = {
  <span class="hljs-attr">isAdmin</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">name</span>: {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"Christian"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"Holm"</span>,
  }
}

<span class="hljs-comment">// Destructuing the nested name property:</span>
<span class="hljs-keyword">const</span> {
    <span class="hljs-attr">name</span>: {
        firstName,
        lastName
    }
} = admin;

<span class="hljs-built_in">console</span>.log(firstName); <span class="hljs-comment">// "John"</span>
<span class="hljs-built_in">console</span>.log(lastName); <span class="hljs-comment">// "Smith"</span>
</code></pre>
<h2 id="heading-array-destructuring">Array destructuring</h2>
<p>Arrays can also be destructured:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> users = [<span class="hljs-string">"Mike"</span>, <span class="hljs-string">"John"</span>, <span class="hljs-string">"Christian"</span>];

<span class="hljs-keyword">const</span> [a, b, c] = users;

<span class="hljs-built_in">console</span>.log(a); <span class="hljs-comment">// "Mike"</span>
<span class="hljs-built_in">console</span>.log(b); <span class="hljs-comment">// "John"</span>
<span class="hljs-built_in">console</span>.log(c); <span class="hljs-comment">// "Christian"</span>
</code></pre>
<h3 id="heading-rest-syntax">Rest syntax</h3>
<p>If you only need some of the array values you can split them up like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> users = [<span class="hljs-string">"Mike"</span>, <span class="hljs-string">"John"</span>, <span class="hljs-string">"Christian"</span>, <span class="hljs-string">"Susan"</span>, <span class="hljs-string">"Joe"</span>];

<span class="hljs-keyword">const</span> [first, second, ...rest] = users;

<span class="hljs-built_in">console</span>.log(first); <span class="hljs-comment">// "Mike"</span>
<span class="hljs-built_in">console</span>.log(second); <span class="hljs-comment">// "John"</span>
<span class="hljs-built_in">console</span>.log(rest); <span class="hljs-comment">// ["Christian", "Susan", "Joe"]</span>
</code></pre>
<h3 id="heading-skipping-values">Skipping values</h3>
<p>You can also skip values with a comma:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> users = [<span class="hljs-string">"Mike"</span>, <span class="hljs-string">"John"</span>, <span class="hljs-string">"Christian"</span>];

<span class="hljs-keyword">const</span> [first, ,third] = users;

<span class="hljs-built_in">console</span>.log(first); <span class="hljs-comment">// "Mike"</span>
<span class="hljs-built_in">console</span>.log(third); <span class="hljs-comment">// "Christian"</span>
</code></pre>
<h3 id="heading-swapping-variables">Swapping variables</h3>
<p>There’s a trick for swapping the values of two or more variables using destructuring:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> a = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">20</span>;

[a, b] = [b, a] 

<span class="hljs-built_in">console</span>.log(a); <span class="hljs-comment">// 20</span>
<span class="hljs-built_in">console</span>.log(b); <span class="hljs-comment">// 10</span>
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p>That's it for this blog post. Hopefully you can see how and why destructuring is a handy new feature in modern JavaScript. It works on both objects and arrays and has a wide, ahem, <em>array</em> of possibilities...</p>
]]></content:encoded></item><item><title><![CDATA[The 'this' keyword]]></title><description><![CDATA[Introduction to this
The this keyword is often a source of confusion for new JavaScript programmers. In this blog post I'll explain what this is, and how it changes depending on use.
This always refers to an object. The object referenced changes depe...]]></description><link>https://christianholm.dev/the-this-keyword</link><guid isPermaLink="true">https://christianholm.dev/the-this-keyword</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Sun, 05 Feb 2023 11:33:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644939589929/n0_RbnLtM.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction-to-this">Introduction to <code>this</code></h2>
<p>The <code>this</code> keyword is often a source of confusion for new JavaScript programmers. In this blog post I'll explain what <code>this</code> is, and how it changes depending on use.</p>
<p><code>This</code> always refers to an object. The object referenced changes depending on the execution context, ie. how you call the function. In other words <code>this</code> references the object of which the function is a property. Let's take a look at what this means:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> user = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"Christian"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"Holm"</span>,
    fullName() {
        <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.firstName}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.lastName}</span>`</span>;
    }
}

user.fullName(); <span class="hljs-comment">// "Christian Holm"</span>
</code></pre>
<p>In the above example, <code>this</code> is used inside a method on the object "user". That's why it points to the user object. But if you were to use <code>this</code> inside a regular function it will behave differently:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">whatIsThis</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);
}

whatIsThis(); <span class="hljs-comment">// returns the global window object</span>
</code></pre>
<p>The reason why is because we're calling whatIsThis() from the global object. It's inferred that what we're really calling is <code>window.whatIsThis()</code> and therefore <code>this</code> refers to the window object. However, if we use the strict mode <code>this</code> will return <code>undefined</code> instead of the window object.</p>
<p>You can use this easy rule to remember: <code>this</code> points to whatever is to the left of the function being called. So user.fullName() points to the user object, but calling a regular function just points to the global object (you can visualize it as <code>window.function()</code>).</p>
<h2 id="heading-arrow-functions">Arrow functions</h2>
<p>Arrow functions behave differently than regular functions. They don’t have their own <code>this</code> context. Instead they get <code>this</code> from their surrounding scope/parent (also known as lexical scope). So if you were to use an arrow function in our previous example, you would get <code>window</code> or <code>undefined</code> depending on whether or not you use strict mode:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> user = {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">"Christian"</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">"Holm"</span>,
    <span class="hljs-attr">fullName</span>: <span class="hljs-function">() =&gt;</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;  <span class="hljs-comment">// simplified the example to show 'this'</span>
    }
}

user.fullName(); <span class="hljs-comment">// Window</span>
</code></pre>
<h2 id="heading-bind">Bind()</h2>
<p>Finally let's talk about <code>bind()</code> and what it does. <code>Bind()</code> creates a new function and binds an object to it. We can then reference the bound object with <code>this</code>. Let's look at an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> john = {
  <span class="hljs-attr">first</span>: <span class="hljs-string">"John"</span>,
  <span class="hljs-attr">last</span>: <span class="hljs-string">"Doe"</span>
}

<span class="hljs-keyword">let</span> susan = {
  <span class="hljs-attr">first</span>: <span class="hljs-string">"Susan"</span>,
  <span class="hljs-attr">last</span>: <span class="hljs-string">"Smith"</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printName</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.first}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.last}</span>`</span>);
}

<span class="hljs-keyword">let</span> johnFunction = printName.bind(john);
johnFunction(); <span class="hljs-comment">// "John Doe"</span>

<span class="hljs-keyword">let</span> susanFunction = printName.bind(susan);
susanFunction(); <span class="hljs-comment">// "Susan Smith"</span>
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p><code>This</code> changes depending on the execution context. Use the 'left of the dot' rule to remember what object the <code>this</code> keyword points to. And remember that arrow functions don't have their own <code>this</code> context, but instead uses its parent context.</p>
]]></content:encoded></item><item><title><![CDATA[Variables in modern JS]]></title><description><![CDATA[Three ways to define a variable
In JavaScript there are three ways to define a variable. There's the old var and the new let and const.
Var can be reassigned multiple times:
var height = 180;
height = 200; // 200

Var doesn’t throw an error if you re...]]></description><link>https://christianholm.dev/variables-in-modern-js</link><guid isPermaLink="true">https://christianholm.dev/variables-in-modern-js</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Thu, 26 Jan 2023 11:33:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644570826186/jjhtREblh3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-three-ways-to-define-a-variable">Three ways to define a variable</h2>
<p>In JavaScript there are three ways to define a variable. There's the old <code>var</code> and the new <code>let</code> and <code>const</code>.</p>
<p><code>Var</code> can be reassigned multiple times:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">var</span> height = <span class="hljs-number">180</span>;
height = <span class="hljs-number">200</span>; <span class="hljs-comment">// 200</span>
</code></pre>
<p><code>Var</code> doesn’t throw an error if you reuse a variable name in the same scope:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">var</span> number = <span class="hljs-number">150</span>;
<span class="hljs-keyword">var</span> number = <span class="hljs-string">"hello, world"</span>; <span class="hljs-comment">// "hello, world"</span>
</code></pre>
<p><code>Var</code> variables are function scoped. That means they’re available everywhere in the scope where they were created (in a function or on the global window object). It can also cause problems if you create a temporary variable for use inside an if statement for example. Since it wasn’t created in a function, it’ll become a global scoped variable:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">var</span> age = <span class="hljs-number">42</span>;
<span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">18</span>) {
    <span class="hljs-keyword">var</span> retirementYears = <span class="hljs-number">68</span> - age;  <span class="hljs-comment">// retirementYears is a global variable!</span>
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`You can retire in <span class="hljs-subst">${retirementYears}</span> years!`</span>)
}

<span class="hljs-built_in">console</span>.log(retirementYears) <span class="hljs-comment">// 26</span>
</code></pre>
<h2 id="heading-let-and-const">let and const</h2>
<p>Which leads us to the two modern ways of using variables. <code>Let</code> and <code>const</code> are both block scoped. That means in the above example, dogYears would be a local variable only accessible from within that if-block:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> age = <span class="hljs-number">42</span>;
<span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">18</span>) {
    <span class="hljs-keyword">let</span> retirementYears = <span class="hljs-number">68</span> - age; <span class="hljs-comment">// retirementYears is now local to this block</span>
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`You can retire in <span class="hljs-subst">${retirementYears}</span> years!`</span>)
}

<span class="hljs-built_in">console</span>.log(retirementYears) <span class="hljs-comment">// ReferenceError: retirementYears is not defined</span>
</code></pre>
<p><code>Let</code> and <code>const</code> will only let you declare a variable once:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"John Smith"</span>;
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"Jane Doe"</span>; <span class="hljs-comment">// SyntaxError: Identifier 'name' has already been declared</span>
</code></pre>
<h2 id="heading-differences-between-let-and-const">Differences between let and const</h2>
<p><code>Let</code> can be reassigned multiple times, where as <code>const</code> can only be assigned once:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Hiro Protagonist"</span>;
name = <span class="hljs-string">"Henry Case"</span>; <span class="hljs-comment">// "Henry Case"</span>

<span class="hljs-keyword">const</span> age = <span class="hljs-number">24</span>;
age = <span class="hljs-number">25</span>; <span class="hljs-comment">// TypeError: Assignment to constant variable</span>
</code></pre>
<p>Note however, that the value that the <code>const</code> variable points to can indeed change (if it’s a mutable type). So if <code>const</code> points to an array, the array itself can be changed without problem:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> ages = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">25</span>, <span class="hljs-number">32</span>];
ages.push(<span class="hljs-number">40</span>); <span class="hljs-comment">// [10, 20, 25, 32, 40]</span>
</code></pre>
<p>But you still can’t assign another value to the <code>const</code> variable:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> ages = [<span class="hljs-number">60</span>, <span class="hljs-number">85</span>] <span class="hljs-comment">// TypeError: Assignment to constant variable</span>
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p>So in conclusion, you’ll want to use <code>let</code> and <code>const</code> instead of <code>var</code>. You might still see <code>var</code> in old code or in tutorials so it’s good to know. But <code>let</code> and <code>const</code> are the only variable types that should be used in modern JavaScript.</p>
<p>You should use <code>let</code> if the variable could be reassigned later, and <code>const</code> if the value isn’t going to change. Some people argue that if in doubt just use <code>const</code> all the time and you’ll get an error if you need to use <code>let</code> instead. But with time it should be easier to figure out which type to use.</p>
]]></content:encoded></item><item><title><![CDATA[Nullish coalescing operator]]></title><description><![CDATA[The nullish coalescing operator ('??') is a handy way to define a default value in case the value you are checking is nullish. A value is nullish if it's either null or undefined. Here's an example of how you would use the nullish coalescing operator...]]></description><link>https://christianholm.dev/nullish-coalescing-operator</link><guid isPermaLink="true">https://christianholm.dev/nullish-coalescing-operator</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Christian Holm]]></dc:creator><pubDate>Sat, 14 Jan 2023 11:34:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644525565767/ZZbTgaXzC.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The nullish coalescing operator ('??') is a handy way to define a default value in case the value you are checking is nullish. A value is nullish if it's either null or undefined. Here's an example of how you would use the nullish coalescing operator:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> user = <span class="hljs-literal">undefined</span>;
<span class="hljs-built_in">console</span>.log(user ?? <span class="hljs-string">'Anonymous'</span>) <span class="hljs-comment">// 'Anonymous'</span>
</code></pre>
<p>If the user variable had been set to 'Jane Smith', the example above would say 'Jane Smith' instead of 'Anonymous':</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> user = <span class="hljs-string">'Jane Smith'</span>;
<span class="hljs-built_in">console</span>.log(user ?? <span class="hljs-string">'Anonymous'</span>) <span class="hljs-comment">// 'Jane Smith'</span>
</code></pre>
<p>So the ?? operator basically returns the first value if it's not null or undefined. But if the first value <em>is</em> null or undefined, it will return the second (right-hand) value. </p>
<p>"Now isn't this what || does?" you might say. Close, but not quite. The logical OR operator || also includes falsy values like 0. Since 0 might be a perfectly valid value to input, using the || operator would lead to bugs since we don't want to use default on a valid number. Using the ?? operator would fix that since it would return the 0 instead of a default value.</p>
]]></content:encoded></item></channel></rss>