<?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: Alaster</title>
    <description>The latest articles on DEV Community by Alaster (@alaster).</description>
    <link>https://dev.to/alaster</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%2F1012560%2F1f706e3f-e3a5-423a-822a-132b49c392e2.jpeg</url>
      <title>DEV Community: Alaster</title>
      <link>https://dev.to/alaster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alaster"/>
    <language>en</language>
    <item>
      <title>I Coded a Website with 60k+ Daily Visitors Using ChatGPT</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Wed, 22 Feb 2023 16:13:55 +0000</pubDate>
      <link>https://dev.to/alaster/i-coded-a-website-with-60k-daily-visitors-using-chatgpt-dmh</link>
      <guid>https://dev.to/alaster/i-coded-a-website-with-60k-daily-visitors-using-chatgpt-dmh</guid>
      <description>&lt;p&gt;ChatGPT, a groundbreaking language model that made its debut at the end of last year, has been increasingly used many different disciplines. Nevertheless, some people complaint the responses from ChatGPT was not very good. The root cause of this issue lies in imprecise and poor-quality prompts.&lt;/p&gt;

&lt;p&gt;To address this problem, we have developed a user-friendly webpage that enables users to copy precise and useful ChatGPT prompts with just one click. These prompts can be modified to fit individual needs. You can paste them into ChatGPT to get helpful responses. We have curated both a English and Chinese version. You can check them out below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts (English Version)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://explainthis.io/zh-hant/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts (Chinese Version)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently this website has around 60k+ daily visitors, and it was built with the assistance of ChatGPT. If you are an engineer, I will offer you valuable insights on how to maximize your productivity with ChatGPT. If you are not an engineer, I would also like to show you that if you can leverage AI tools, you can complete your own projects without the need for a dedicated engineer. With a step-by-step approach, you can also implement the desired functionality by yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips for using ChatGPT to write programs
&lt;/h2&gt;

&lt;p&gt;When utilizing ChatGPT to help you write code, there are several important things to keep in mind, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Choice of programming language:&lt;/strong&gt; To inform ChatGPT which language and framework to use, you can use the prompt &lt;code&gt;Act as an expert of [a particular programming language]&lt;/code&gt;, such as &lt;code&gt;Act as an expert of python&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specify a paradigm or design pattern:&lt;/strong&gt; If you have a specific design pattern or paradigm in mind, be sure to indicate it. As there are many different ways to achieve a certain functionality, it is essential to provide ChatGPT with this instruction if you want it to write code that aligns with your coding style. For instance, the prompt &lt;code&gt;Implement [a certain functionality] using functional programming&lt;/code&gt; can instruct ChatGPT to write that functionality with functional programming.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear description:&lt;/strong&gt; It is crucial to provide a clear and concise description of your logic. Although ChatGPT is powerful, the reality is ChatGPT cannot read your mind. Thus, your logic must be conveyed explicitly and coherently for ChatGPT to write the code that meets your expectations. Unclear or erroneous instructions may result in ChatGPT failing to write the desired code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Provide examples:&lt;/strong&gt; Providing examples can help ChatGPT better understand what you are looking for. For example, if you need ChatGPT to help you write a function, you can provide an input and the expected output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write tests:&lt;/strong&gt; When writing codes, it is crucial to test the code yourself to ensure that it functions as expected. If you prefer to automate the testing process, you can use ChatGPT to write test cases with the prompt &lt;code&gt;Write 5 test cases for [a section of code]&lt;/code&gt; and run those test cases to verify that everything is working as expected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have reviewed the essential considerations for using ChatGPT to write code, let us explore how we can leverage this powerful tool to actually help us build our website. To keep this article concise, we will showcase three practical examples that ChatGPT successfully write the program. These examples are representative in that they require a level of proficiency in React and JavaScript that may take quite some time for beginner programmers to implement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement &lt;code&gt;highlightText&lt;/code&gt; function
&lt;/h2&gt;

&lt;p&gt;When visiting &lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts&lt;/a&gt; website,&lt;br&gt;
as you can see from each prompt, we have highlighted the words that need to be replaced with a light blue color.&lt;/p&gt;

&lt;p&gt;But how did we manage to accomplish this task? Of course, we did not manually highlight every word in every prompt. If there were a thousand prompts, it would be impractical to do so. From a programming perspective, we can parse a specific format and automatically convert it into highlighted words.&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%2Fewqhap62auapn6la33vz.png" 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%2Fewqhap62auapn6la33vz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how might we do this? How can we automatically highlight the programming language in the sentence &lt;code&gt;You are now an expert in [a programming language]&lt;/code&gt;? Without ChatGPT, we would likely need to do the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First of all, we would need to identify whether the text in &lt;code&gt;[]&lt;/code&gt; match a specific pattern in a given string.&lt;/li&gt;
&lt;li&gt;This is typically done by using regular expressions (Regex), but since I am not well-versed in Regex, I would probably need to check out StackOverflow, a website frequently used by developers to seek answers, and try different solutions on StackOverflow to achieve it.&lt;/li&gt;
&lt;li&gt;After identifying the pattern of &lt;code&gt;[]&lt;/code&gt;, we would need to write a function to replace the text in &lt;code&gt;[]&lt;/code&gt; with the highlighted format.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, if you can articulate your ideas, ChatGPT can write code based on those ideas. Here is what I wrote to ChatGPT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please help me write a highlightText function in TypeScript that accepts a string with [text] and detects the text inside the brackets. The function should replace the text inside the brackets with &lt;code&gt;&amp;lt;span className="bg-sky-100 text-slate-800"&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
Input: &lt;code&gt;You are now a [programming language] expert&lt;/code&gt;&lt;br&gt;
Output: &lt;code&gt;You are now a &amp;lt;span className="bg-sky-100 text-slate-800"&amp;gt;programming language&amp;lt;/span&amp;gt; expert&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I was amazed by what ChatGPT wrote. It used the correct regular expression &lt;code&gt;/\[(.*?)\]/g;&lt;/code&gt; and then used &lt;code&gt;split&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; methods to replace each set of &lt;code&gt;[]&lt;/code&gt; with the highlighted text. In less than five minutes, I was able to accomplish this &lt;code&gt;highlightText&lt;/code&gt; function.&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%2Fde0ajj3nn3wzwsvrkesx.png" 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%2Fde0ajj3nn3wzwsvrkesx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Scroll Back To Top Button
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts&lt;/a&gt;, if you scroll down, you will notice a round button with an upward arrow in the bottom right corner of the web page. This is a common feature on many websites, and when clicked, the page automatically scrolls back up to the top. How can we implement this common functionality? Let us ask ChatGPT for help.&lt;/p&gt;

&lt;p&gt;Here is the prompt I gave ChatGPT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are now an expert in TypeScript, React, and TailwindCSS. Today, I want to utilize these three tech stacks to create a component. This component is a round button that appears in the bottom right corner of a page only after scrolling down. If the page is at the top, the button should not be visible. When the user clicks on the button, the page should scroll back to the top. Once the page reaches the top, the button should be hidden until the user scrolls down again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let us see the code ChatGPT wrote for us&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%2F4txnmrfl3x1mqfijnqm5.png" 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%2F4txnmrfl3x1mqfijnqm5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the entire program is a bit too long, I only screenshot the top half. But you can see, it took ChatGPT less than 1 minute to implement this component. ChatGPT used &lt;code&gt;const [showButton, setShowButton] = useState(false);&lt;/code&gt; to manage the state, and use &lt;code&gt;window.pageYOffset &amp;gt; 0&lt;/code&gt; to conditionally show the button.&lt;/p&gt;

&lt;p&gt;As the code was written in React, ChatGPT recognized that if it needed to interact with external systems outside of React (in this case, the runtime of the browser), the logic must be written in &lt;code&gt;useEffect&lt;/code&gt;. Additionally, since a &lt;code&gt;scroll&lt;/code&gt; listener was registered here, it was imperative to remove this listener at the end of the React component lifecycle to avoid memory leaks. This is something many React beginners forget to do, but ChatGPT did not forget. It correctly used &lt;code&gt;removeEventListener&lt;/code&gt; to remove the listener.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleScroll&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleScroll&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&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;



&lt;p&gt;You can try it out for yourself by entering the prompt above into ChatGPT. You will find that you too can create the common scroll-to-top button that is frequently seen on many websites - it is that simple!😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Filtering by Category
&lt;/h2&gt;

&lt;p&gt;The last example is also a very common one in many website. In &lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts&lt;/a&gt; you can see, there is a row of ChatGPT-colored categories. When you click on a category, the website automatically filters the content to show only items belonging to that category. This functionality can be implemented in two common ways.&lt;/p&gt;

&lt;p&gt;For smaller data sets, all data is stored on the frontend, and filtering is done on the client-side. For larger data sets, data is stored in a database, and when a user clicks on a category, the category parameter is passed to the backend for filtering. The filtered data is then sent back to the frontend for display.&lt;/p&gt;

&lt;p&gt;Since in &lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts&lt;/a&gt;, currently, we do not have so many prompts that require storing them in a separate database (after testing it, we found that making an API call to retrieve the data would add too much network latency for now). Therefore, we temporarily store all the prompts on the frontend and perform filtering on the client-side.&lt;/p&gt;

&lt;p&gt;Let us take a look at the prompt I entered and the codes written by ChatGPT. Once again, in less than a minute, ChatGPT has successfully implemented the functionality I requested. Also, variable naming was clear and easy to understand, very impressive 😎&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%2Fotcvm5915sw9m661phcc.png" 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%2Fotcvm5915sw9m661phcc.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Final Thoughts — Rapid Prototyping
&lt;/h2&gt;

&lt;p&gt;Building &lt;a href="https://explainthis.io/en/chatgpt" rel="noopener noreferrer"&gt;Useful ChatGPT Prompts&lt;/a&gt; took much less time compared to coding my other side projects. The time spent on developing a website prototype is significantly lower with the help from Copilot and ChatGPT. This means that you can turn your ideas and creativity into reality in a much faster way. So what are you waiting for? Let us start building and make the world a better place with the help of ChatGPT!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Test Your JavaScript Knowledge with These 12 Basic Quizzes!</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Mon, 30 Jan 2023 09:31:36 +0000</pubDate>
      <link>https://dev.to/alaster/checking-your-familiarity-with-12-js-fundamentals-c61</link>
      <guid>https://dev.to/alaster/checking-your-familiarity-with-12-js-fundamentals-c61</guid>
      <description>&lt;p&gt;Do you know JavaScript? If yes, how well are you familiar with JavaScript? In this article, we will go through some common JavaScript questions. Try them out to see your level of familiarity with JavaScript 😎&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables

&lt;ul&gt;
&lt;li&gt;What is the differences between &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;How to compare two values? What is the difference &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;===&lt;/code&gt; and &lt;code&gt;Object.is&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Data types

&lt;ul&gt;
&lt;li&gt;What are data types in JavaScript?&lt;/li&gt;
&lt;li&gt;What is the difference between Map and Object?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Array

&lt;ul&gt;
&lt;li&gt;What is the difference between &lt;code&gt;slice&lt;/code&gt;, &lt;code&gt;split&lt;/code&gt; and &lt;code&gt;splice&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Object

&lt;ul&gt;
&lt;li&gt;What is &lt;code&gt;this&lt;/code&gt;? What is the difference between &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Can you explain how &lt;code&gt;new&lt;/code&gt; operator work?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Function

&lt;ul&gt;
&lt;li&gt;What is arrow function?&lt;/li&gt;
&lt;li&gt;What is IIFE (Immediately invoked function expression)?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Class

&lt;ul&gt;
&lt;li&gt;What is the difference between class and constructor in JavaScript?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Closures

&lt;ul&gt;
&lt;li&gt;What is closure?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Prototypes

&lt;ul&gt;
&lt;li&gt;What is prototypes?&lt;/li&gt;
&lt;li&gt;What is prototypal inheritance?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Scope

&lt;ul&gt;
&lt;li&gt;What is scope in JavaScript?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Hoisting

&lt;ul&gt;
&lt;li&gt;What is Hoisting?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Promise/Async await

&lt;ul&gt;
&lt;li&gt;What is Promise?&lt;/li&gt;
&lt;li&gt;What is &lt;code&gt;Promise.race&lt;/code&gt; and &lt;code&gt;Promise.all&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;ES6+

&lt;ul&gt;
&lt;li&gt;What is destructuring in JavaScript and how does it work in ES6?&lt;/li&gt;
&lt;li&gt;Are you aware of the latest updates and features in the latest ES version?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  If you're unable to answer it effectively, these helpful links may assist you in finding the solution.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.explainthis.io/" rel="noopener noreferrer"&gt;ExplainThis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.info/" rel="noopener noreferrer"&gt;JavaScript Info&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/getify/You-Dont-Know-JS" rel="noopener noreferrer"&gt;You Don't Know JS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;If you like the content, check out the free frontend interview prep resources my friends and I curated at &lt;a href="https://www.explainthis.io/" rel="noopener noreferrer"&gt;explainthis.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have got frontend engineer offers from both startup and FAANG companies. I would like to help as many people as possible to land a job. If you have any questions regarding to frontend engineering or frontend interview. Feel free to reach out to me at &lt;a href="mailto:hello@explainthis.io"&gt;hello@explainthis.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>What is the difference between ==, === and Object.is() in JavaScript</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Thu, 26 Jan 2023 12:42:54 +0000</pubDate>
      <link>https://dev.to/alaster/what-is-the-difference-between-and-objectis-in-javascript-nel</link>
      <guid>https://dev.to/alaster/what-is-the-difference-between-and-objectis-in-javascript-nel</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The post was originally published on &lt;a href="https://www.explainthis.io/"&gt;ExplainThis&lt;/a&gt; with title &lt;a href="https://www.explainthis.io/en/interview-guides/javascript/js-equality"&gt;《What is the difference between ==, === and Object.is() in JavaScript》&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many ways to compare equality in JavaScript. The three most common are &lt;code&gt;===&lt;/code&gt; (strict comparison), &lt;code&gt;==&lt;/code&gt; (loose comparison), and &lt;code&gt;Object.is&lt;/code&gt; (equal comparison). What's the difference between the three? Let's take a look at this question that is often asked in interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;==&lt;/code&gt; Loose comparison (loose equality)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When using &lt;code&gt;==&lt;/code&gt;, before two values are compared, the type will be converted first.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, in the following three examples, the reason why &lt;code&gt;true&lt;/code&gt; will be printed is precisely because before the comparison, the type is converted.&lt;br&gt;
Otherwise, the number &lt;code&gt;1&lt;/code&gt; and the string &lt;code&gt;'1'&lt;/code&gt; represent different values in JavaScript, and strictly speaking, they should not be equal.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt; are different types and different values in JavaScript, but if &lt;code&gt;==&lt;/code&gt; is used, &lt;code&gt;true&lt;/code&gt; will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;==&lt;/code&gt; can cause some confusion for developers because of the type conversion. Therefore, &lt;strong&gt;in most cases, it is not recommended to use &lt;code&gt;==&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;===&lt;/code&gt; Strict comparison (strict equality)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;===&lt;/code&gt; does not convert the type and value, so comparing the two will return &lt;code&gt;false&lt;/code&gt; if they are of different types.&lt;br&gt;
If the value is different, &lt;code&gt;false&lt;/code&gt; will also be returned.&lt;/p&gt;

&lt;p&gt;There are two exceptions though, strict comparisons will return &lt;code&gt;true&lt;/code&gt; when we compare &lt;code&gt;+0&lt;/code&gt; and &lt;code&gt;-0&lt;/code&gt;, and comparisons &lt;code&gt;NaN&lt;/code&gt; and &lt;code&gt;NaN&lt;/code&gt; will return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;These two situations are when equality comparison &lt;code&gt;Object.is&lt;/code&gt; comes in handy.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Object.is&lt;/code&gt; Same-value comparison (same-value equality)
&lt;/h2&gt;

&lt;p&gt;Same-value comparison, as the name implies, compares whether two values are equal.&lt;br&gt;
Although it starts with Object, any two values can be compared. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above two problems in &lt;code&gt;===&lt;/code&gt; can be fixed by using &lt;code&gt;Object.is&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if you want to check the equality of &lt;code&gt;NaN&lt;/code&gt;, there is a method called &lt;code&gt;isNaN&lt;/code&gt; in JavaScript. Whether to use it depends on the habits of the development team. If you are unfamiliar with &lt;code&gt;Object.is&lt;/code&gt;, you can choose to use &lt;code&gt;Number.isNaN&lt;/code&gt;.&lt;/p&gt;




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

&lt;p&gt;If you like the content, check out the free frontend interview prep resources my friends and I curated at &lt;a href="https://www.explainthis.io/"&gt;explainthis.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have got frontend engineer offers from both startup and FAANG companies. I would like to help as many people as possible to land a job. If you have any questions regarding to frontend engineering or frontend interview. Feel free to reach out to me at &lt;a href="mailto:hello@explainthis.io"&gt;hello@explainthis.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My TikTok (ByteDance) Frontend Software Engineer Interview Experience</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Wed, 25 Jan 2023 14:10:22 +0000</pubDate>
      <link>https://dev.to/alaster/my-tiktok-bytedance-frontend-software-engineer-interview-experience-dbn</link>
      <guid>https://dev.to/alaster/my-tiktok-bytedance-frontend-software-engineer-interview-experience-dbn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The post was originally published on &lt;a href="https://www.explainthis.io"&gt;ExplainThis&lt;/a&gt; with title 《&lt;a href="https://www.explainthis.io/en/software-engineer-note/software-engineer-career/my-tiktok-frontend-interview"&gt;My TikTok (ByteDance) Frontend Software Engineer Interview Experience&lt;/a&gt;》&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a blog post on my interview experience with TikTok in summer 2022. The role I interviewed for was &lt;strong&gt;Frontend Software Engineer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I applied this frontend software engineer position through TikTok's career website, but somehow I was contacted by a ByteDance recruiter. It's because ByteDance is TikTok's parent company. It's kinda like applying Facebook but get contacted by Meta a recruiter. I must say ByteDance's efficiency was very impressive. I submitted my resume in the morning and received an interview invitation in the afternoon in the same day.&lt;/p&gt;

&lt;p&gt;After each round of interview, I was arranged the next round within an hour. It took only two weeks from application to finishing all interviews. ByteDance is already a company with more than 100K employees, and it's very impressive that they still maintain this kind of efficiency and speed. Because ByteDance did not specifically sign a confidentiality clause for the interview part, and most people directly share the questions they did on the Internet, I will also directly share the questions I was asked.&lt;/p&gt;




&lt;h2&gt;
  
  
  First Round
&lt;/h2&gt;

&lt;p&gt;The interviewer was a senior frontend engineer. The interviewer asked me to do a self-introduction first. Then he started asking questions about past projects I put on the resume, and he followed up with "&lt;strong&gt;what is the most challenging project you have done&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;After the resume question, he asked questions related to frontend. The questions included&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What Hooks have you used?&lt;/li&gt;
&lt;li&gt;Talk about the detail of &lt;code&gt;useEffect&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What is the dependency array for? How does it work?&lt;/li&gt;
&lt;li&gt;Why does &lt;code&gt;useEffect&lt;/code&gt; only execute once when the component is mounted if the dependency array is empty?&lt;/li&gt;
&lt;li&gt;What happens if you put an object or an array in the dependency array?&lt;/li&gt;
&lt;li&gt;What happens if a &lt;code&gt;ref&lt;/code&gt; is passed in the dependency array?&lt;/li&gt;
&lt;li&gt;What is the difference between &lt;code&gt;useLayoutEffect&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;? When should you which?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the React questions, he asked basic networking questions, mainly for HTTP&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.explainthis.io/en/interview-guides/browser/http-caching"&gt;Please talk about HTTP caching.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Between &lt;code&gt;eTag&lt;/code&gt; or &lt;code&gt;Last Modified&lt;/code&gt;, which one is preferred? Why?&lt;/li&gt;
&lt;li&gt;What is the response body of 304?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After network questions, he asked me JavaScript questions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First is on Event Loop, Microtasks, and Macrotasks, (&lt;a href="https://www.explainthis.io/en/interview-guides/javascript/js-event-loop-questions"&gt;see this article for details&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Second is a JavaScript whiteboard question, which was to implement &lt;code&gt;.get&lt;/code&gt; of lodash.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Second Round
&lt;/h2&gt;

&lt;p&gt;The interviewer was a senior frontend engineer. After self-introduction, he asked questions on my resume, and asked me about my experience in writing unit tests and E2E tests in the past. Then he asked a few JavaScript questions, including&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the difference between a Map object in JavaScript and a general object?&lt;/li&gt;
&lt;li&gt;What is the current trend of JavaScript?&lt;/li&gt;
&lt;li&gt;What is new in React 18&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This round ended with a whiteboard question, which was a LeetCode Medium string question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Third Round
&lt;/h2&gt;

&lt;p&gt;This interview is not the same as the previous two rounds. There was no self-introduction. The interviewer asked a LeetCode Medium question in the very beginning, which is basically a variation of 3Sum. After that, he asked me a few frontend questions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What problems does React Fiber solve?&lt;/li&gt;
&lt;li&gt;How does HTTP work?&lt;/li&gt;
&lt;li&gt;What is the s in HTTPs?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, there is a front-end system design question. It took about 20 minutes to discuss how to design a Typeahead front-end.&lt;/p&gt;

&lt;p&gt;After the three technical interviews, I feel that they pretty much covered everything from JavaScript, React, Networking, algorithm and system design. So if you want to prepare for the TikTok frontend interview, you need to prepare a lot!&lt;/p&gt;

&lt;h2&gt;
  
  
  Fourth Round (Human Resources)
&lt;/h2&gt;

&lt;p&gt;After successfully passing the three rounds of technical interviews, I finally entered the HR round. The HR first introduced the culture of ByteDance and what the team I interviewed was doing. Many people shared on the internet that you should not take this round lightly, and you must prepare well for various behavioral interview questions. The question asked by HR was mainly to see your fit with the company culture, so it will be helpful to read the company culture in advance.&lt;/p&gt;

&lt;p&gt;I received the offer one week after the HR round.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>My Journey from Designer to Big Tech Frontend Engineer</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Tue, 24 Jan 2023 13:58:14 +0000</pubDate>
      <link>https://dev.to/alaster/my-journey-from-liberal-arts-degree-to-big-tech-frontend-engineering-52fa</link>
      <guid>https://dev.to/alaster/my-journey-from-liberal-arts-degree-to-big-tech-frontend-engineering-52fa</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The post was originally published on &lt;a href="https://www.explainthis.io/" rel="noopener noreferrer"&gt;ExplainThis&lt;/a&gt; with title 《&lt;a href="https://www.explainthis.io/en/software-engineer-note/software-engineer-career/how-i-turn-software-engineer" rel="noopener noreferrer"&gt;My Journey from Designer to Big Tech Frontend Engineer&lt;/a&gt;》&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a blog post on my experience from a designer to a frontend engineer at a big tech company. I pivoted my career three years ago. And now I am working at a big tech company as a frontend engineer. My journey wasn't smooth-sailing, and I hope it gave you a more realistic picture on switching career to frontend engineering.&lt;/p&gt;




&lt;h2&gt;
  
  
  My motivation to learn coding
&lt;/h2&gt;

&lt;p&gt;When I was a student, I chose to major in Econ. I didn't choose it because I loved it, but because I didn't know what to choose. After declaring my major for a year, I regretted it. I wasn't interested in the classes I took. Most of my friends went to banking or consulting for internship, but I didn't think those were for me. &lt;/p&gt;

&lt;p&gt;A pivotal moment in my college life was that I took a design course, and I absolutely loved it. I loved that I could design something out of nothing, and design a solution to solve a problem. That's why I decided to start my career in design after graduating from college. &lt;/p&gt;

&lt;p&gt;Working in the field of design, I sometimes needed to work with engineers, especially with frontend engineers. When I worked with them, I always felt they were cool, because they were doing something I had no idea about. I'd never learnt anything related to coding and programming. I couldn't understand any line of codes they wrote. Nevertheless, it's still fascinating to me because my frontend engineers colleges always turned my design idea into reality. Without the help from them, my design wouldn't be able to interact with the real users. This experience planted a seed in me. I started to think about learning how to code. &lt;/p&gt;

&lt;h2&gt;
  
  
  How I got started
&lt;/h2&gt;

&lt;p&gt;In the very beginning 2019, I asked my frontend engineer colleges what to learn. They recommended me HTML, CSS, and JavaScript. In retrospect, I think this is generally a good recommendation. There are many programming languages you can learn. But if you want to work on the frontend, you probably don't need to learn Java or Python despite their popularity. &lt;/p&gt;

&lt;p&gt;I bought a few books on HTML, CSS, and JavaScript. However, reading books are not very helpful to me. So I tried to search for different learning materials. That's when I found &lt;a href="https://www.freecodecamp.org/" rel="noopener noreferrer"&gt;freeCodeCamp&lt;/a&gt;. I like that in freeCodeCamp, you learn coding in an interactive environment. I think this is particularly important. You must learn coding by doing. Reading books or watching tutorials are not enough. In fact, there is a term called &lt;em&gt;Tutorial Hell&lt;/em&gt;. It refers to people who watch a lot of tutorials but code nothing It's like a hell because you would never truly learn how to code by doing so. &lt;/p&gt;

&lt;h2&gt;
  
  
  Committing myself to frontend engineering
&lt;/h2&gt;

&lt;p&gt;After around 6 months of learning during night and weekend, I felt I've grasped the basic idea of coding. I started to think about switching my career from design to frontend engineering. However, I wasn't very sure about the jump. So I took a safe route, I went to a part-time online coding bootcamp. It's part-time so I didn't need to quit my day job, but it's still very intense. &lt;/p&gt;

&lt;p&gt;When I was in the bootcamp, I felt very tired every day. After working for 8 hours a day, there wasn't much energy left for the assignments and projects from the bootcamp. But I didn't give up, because I thought of it as a test. I wanna to see if I really like frontend engineering. If I gave up, then it meant I probably didn't like it as much as I thought. I always like the brick walls quote from Randy Pausch. It was what I told myself during the coding bootcamp. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Brick walls are there for a reason. The brick walls are not there to keep us out. The brick walls are there to show how badly we want something. Because the brick walls are there to stop the people who don't want something badly enough." -- Randy Pausch&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By the end of the bootcamp, I built two projects and put them on my resume. One was a simple Twitter clone, and the other was a simple Yelp clone. I built them using JavaScript with React for the frontend, and Node.js with Express for the backend. Since my goal was to get a frontend engineering job, I put much more time and effort on the frontend part, making sure both the design and the code look good for the frontend. &lt;/p&gt;

&lt;h2&gt;
  
  
  Imposter Syndrome in my first job
&lt;/h2&gt;

&lt;p&gt;I was able to secure a frontend engineering job after graduating from the bootcamp in March 2020. I have to say, getting a job is just a beginning. Compared with my time at bootcamp, it was much harder in the first few months in my first frontend engineering job. In my first job, I experienced what people call Imposter Syndrome. I still remember I received more than 20 comments in my first pull request. It made me feel like I did not know how to write code. &lt;/p&gt;

&lt;p&gt;If you are feeling the same in your first software engineering job, don't worry about it. I know it's hard, but it's only temporary. When you gradually get yourself familiar with the codebase and coding convention in the team, things will get better. Trust me! &lt;/p&gt;

&lt;h2&gt;
  
  
  First FAANG offer
&lt;/h2&gt;

&lt;p&gt;In 2022, I got multiple offers from different big tech companies, including FAANG companies (Amazon) and the largest unicorn startup in the world (TikTok). But I am not going to sugar coat it. It wasn't smooth at all. In fact, I gave big tech companies a try in 2021 after gaining two years of full-time experience in frontend development. But I got 0 offer. &lt;/p&gt;

&lt;p&gt;That's why I spent almost a year, preparing both LeetCode style interviews and frontend interviews. That's also the reason why I built &lt;a href="https://explainthis.io" rel="noopener noreferrer"&gt;ExplainThis&lt;/a&gt; with my friends. ExplainThis is a free interview prep website, aiming to help people like me to prepare their interview well. We've curated the most common interview questions and solutions, and we share them for free. I believe if a normal guy with only a liberal arts degree like me can get an offer from the big tech company, you can do it too! &lt;/p&gt;

&lt;h2&gt;
  
  
  Struggle optimistically
&lt;/h2&gt;

&lt;p&gt;It's a long 3-year journey, but when I look back, there wasn't a single moment that I regret. Even though I am in a big tech company now, I still feel struggling all the time. Especially seeing the recent layoffs from almost all big tech companies, I feel very insecure to be honest.  But I know, I cannot control what I cannot control. What I can do is keep learning and growing. &lt;/p&gt;

&lt;p&gt;I'm optimistic though. Software is still eating the world. Even though most big tech companies are cutting jobs, there are still many opportunities in non-big-tech companies. So if you are also struggling now, let's struggle optimistically together. And if there is anything I can help you with, send me an email at &lt;a href="mailto:hello@explainthis.io"&gt;hello@explainthis.io&lt;/a&gt; 😊&lt;/p&gt;

</description>
      <category>ux</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>What array traversal methods are there in JavaScript? (for loop, for...in, for...of, forEach, map, filter, every, some)</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Mon, 23 Jan 2023 11:50:44 +0000</pubDate>
      <link>https://dev.to/alaster/what-array-traversal-methods-are-there-in-javascript-for-loop-forin-forof-foreach-map-filter-every-some-mpm</link>
      <guid>https://dev.to/alaster/what-array-traversal-methods-are-there-in-javascript-for-loop-forin-forof-foreach-map-filter-every-some-mpm</guid>
      <description>&lt;p&gt;In basic JavaScript interview, the method of traversing the array is often asked. &lt;/p&gt;

&lt;p&gt;This article lists the most commonly used array traversal methods in JavaScript, including &lt;strong&gt;&lt;code&gt;for&lt;/code&gt; loops&lt;/strong&gt;, native methods of arrays &lt;strong&gt;(&lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;...)&lt;/strong&gt;, and common array traversal questions in interviews.&lt;/p&gt;

&lt;p&gt;This article is divided into two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;8 ways to iterate over the array&lt;/li&gt;
&lt;li&gt;What is the difference between &lt;code&gt;for...of&lt;/code&gt; and &lt;code&gt;for...in&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Traversing an array in 8 different ways
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for" rel="noopener noreferrer"&gt;&lt;code&gt;for&lt;/code&gt;&lt;/a&gt; loop
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can use &lt;code&gt;break&lt;/code&gt; to end the loop or use &lt;code&gt;continue&lt;/code&gt; to jump out of the current loop&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Imperative writing is more verbose&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// 0 1 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of" rel="noopener noreferrer"&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This is a ES6 feature. It is more concise than the &lt;code&gt;for&lt;/code&gt; loop&lt;/li&gt;
&lt;li&gt;Like the &lt;code&gt;for&lt;/code&gt; loop, it can be used with &lt;code&gt;break&lt;/code&gt;, &lt;code&gt;continue&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in" rel="noopener noreferrer"&gt;&lt;code&gt;for...in&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When using &lt;code&gt;for...in&lt;/code&gt;, &lt;code&gt;key:value&lt;/code&gt; is being iterated, and the &lt;code&gt;key&lt;/code&gt; in an array is the &lt;code&gt;index&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Below are the native methods you can use to iterate an array&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" rel="noopener noreferrer"&gt;&lt;code&gt;forEach&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;forEach&lt;/code&gt; will execute the callback function for each element&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;forEach&lt;/code&gt; method will only iterate over the original array and will not return a new array. 
So if you need to construct a new array from an old array, you should use the &lt;code&gt;map&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;The declarative writing method is preferred by many developers, 
but you cannot use &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; syntax to jump out of the loop in &lt;code&gt;forEach&lt;/code&gt;, as shown in image figure below&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnqve8cri033e5rpqnmr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnqve8cri033e5rpqnmr1.png" alt="Uncaught Syntax" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;&lt;code&gt;map&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; executes the provided callback function for each element and returns an new array&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;&lt;code&gt;filter&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;filter&lt;/code&gt; returns a new array, and filter out elements that do not pass the condition in the provided callback function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every" rel="noopener noreferrer"&gt;&lt;code&gt;every&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;every&lt;/code&gt; will check whether each element in the array passes the condition in the provided callback function, and finally returns a &lt;code&gt;Boolean&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If every element passes, it will return &lt;code&gt;true&lt;/code&gt;, but if one of the element does not meet is &lt;code&gt;false&lt;/code&gt;, it will end early and return &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The difference with &lt;code&gt;some&lt;/code&gt; is that &lt;code&gt;every&lt;/code&gt; method requires that &lt;strong&gt;every&lt;/strong&gt; element in the array must pass the condition. But for &lt;code&gt;some&lt;/code&gt;, only one element needs to pass the condition.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some" rel="noopener noreferrer"&gt;&lt;code&gt;some&lt;/code&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The method is similar to &lt;code&gt;every&lt;/code&gt;, it will test whether each element of the array passes the condition of the provided callback function, 
if one of the element meets the condition, it will end early and return &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The difference with &lt;code&gt;every&lt;/code&gt; is that with the &lt;code&gt;some&lt;/code&gt; method, &lt;strong&gt;as long as there is an element&lt;/strong&gt; in the array that meets the condition in callback function, it will return &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Bonus question: Which methods will change the original array?
&lt;/h2&gt;

&lt;p&gt;The array methods mentioned above will not directly change the original array by default, unless the function we pass in does some processing on the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus question: Which methods will return a new array?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt; will return a new array, so if you are writing immutable code in React, you often need to use these two methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus question: What is the difference between &lt;code&gt;for&lt;/code&gt; loop and &lt;code&gt;forEach&lt;/code&gt;?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There is a difference in wording, &lt;code&gt;forEach&lt;/code&gt; is more concise&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;forEach&lt;/code&gt; cannot end the entire interation early, nor can it be used with &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; syntax. Also using &lt;code&gt;return&lt;/code&gt; will also be ignored in &lt;code&gt;forEach&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bonus question: What is the difference between &lt;code&gt;for...of&lt;/code&gt; and &lt;code&gt;for...in&lt;/code&gt;?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;for...of&lt;/code&gt; iterates the &lt;code&gt;element value (value)&lt;/code&gt; in the array; and &lt;code&gt;for...in&lt;/code&gt; iterates the &lt;code&gt;key: value&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//10 20 30&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//0 1 2&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In most cases, &lt;code&gt;for...in&lt;/code&gt; will be used to iterate objects, but it is not recommended to use &lt;code&gt;for...in&lt;/code&gt; to iterate arrays, &lt;strong&gt;the main reasons are as follows:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. When there is an empty item in the array, using the &lt;code&gt;for...in&lt;/code&gt; method ignores the item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 0 2&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// a undefined c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b. &lt;code&gt;for... in&lt;/code&gt; will check whether the properties of the object are enumerable. &lt;/p&gt;

&lt;p&gt;If true, it will iterate out all the names of these properties. &lt;/p&gt;

&lt;p&gt;Since some JavaScript libraries may create methods on the Array prototype, if you use &lt;code&gt;for...in&lt;/code&gt; method, you may operate on values that are not present in the array. But using &lt;code&gt;for...of&lt;/code&gt; can avoid this issue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 0 1 2 foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c. Although the &lt;strong&gt;key:value&lt;/strong&gt; iterated by &lt;code&gt;for…in&lt;/code&gt; is &lt;code&gt;index&lt;/code&gt;, &lt;code&gt;for…in&lt;/code&gt; will use the String type as the &lt;strong&gt;key:value&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;So if we take &lt;code&gt;index&lt;/code&gt; to do calculations may cause unexpected results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// 01 11 21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d. The iteration of &lt;code&gt;for...in&lt;/code&gt; does not guarantee that the order is correct. &lt;/p&gt;

&lt;p&gt;This situation is very difficult to use on an array, because when we want to iterate the values in the array, we usually want the order to be correct.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;for...of&lt;/code&gt; will first check the &lt;code&gt;[Symbol.iterator]&lt;/code&gt; property of the object, and then use &lt;code&gt;[Symbol.iterator].next()&lt;/code&gt; to iterate out the values one by one to ensure that the order is correct. &lt;/p&gt;

&lt;p&gt;But since &lt;code&gt;Object&lt;/code&gt; does not have the property &lt;code&gt;[Symbol.iterator]&lt;/code&gt;, &lt;code&gt;for...of&lt;/code&gt; cannot be used on Object.&lt;/p&gt;




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

&lt;p&gt;If you like the content, check out the free frontend interview prep resources my friends and I curated at &lt;a href="https://www.explainthis.io/" rel="noopener noreferrer"&gt;explainthis.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have got frontend engineer offers from both startup and FAANG companies. I would like to help as many people as possible to land a job. If you have any questions regarding to frontend engineering or frontend interview. Feel free to reach out to me at &lt;a href="mailto:hello@explainthis.io"&gt;hello@explainthis.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>tooling</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Will passing (a,b) =&gt; b - a in JavaScript builit-in sort a be in ascending or descending order? Why?</title>
      <dc:creator>Alaster</dc:creator>
      <pubDate>Sun, 22 Jan 2023 02:29:43 +0000</pubDate>
      <link>https://dev.to/alaster/will-passing-ab-b-a-in-javascript-builit-in-sort-a-be-in-ascending-or-descending-order-why-1f04</link>
      <guid>https://dev.to/alaster/will-passing-ab-b-a-in-javascript-builit-in-sort-a-be-in-ascending-or-descending-order-why-1f04</guid>
      <description>&lt;p&gt;Arrays in JavaScript have a built-in sort method, which allows us to have a high-performance sorting without writing our own sorting algorithm. However, do you know how to use sort? How does the compareFn passed in sort work? &lt;strong&gt;When compareFn is (a, b) =&amp;gt; b - a, will the sort be ascending or descending?&lt;/strong&gt; Why?&lt;/p&gt;

&lt;p&gt;This is not only commonly used in work, but also a frequently asked question in interviews. &lt;/p&gt;

&lt;p&gt;Make sure you have a good understanding of this concept. If you are not familiar with it, let us find out through this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use &lt;code&gt;sort&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="noopener noreferrer"&gt;MDN documentation&lt;/a&gt;, you can see that &lt;code&gt;sort&lt;/code&gt; is used like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;compareFn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Among them, &lt;code&gt;compareFn&lt;/code&gt; requires two parameters &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. In the comparison rules, it must return &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;-1&lt;/code&gt;. The rules to be followed are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;compareFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;greater&lt;/span&gt; &lt;span class="nx"&gt;than&lt;/span&gt; &lt;span class="nx"&gt;b&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// means a is placed after b&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;smaller&lt;/span&gt; &lt;span class="nx"&gt;than&lt;/span&gt; &lt;span class="nx"&gt;b&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// means a is placed before b&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// a is equivalent to b&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// means the position between a and b remains unchanged&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when writing comparisons between numbers, we tend to write a little more concisely. &lt;/p&gt;

&lt;p&gt;In the following example, &lt;code&gt;compareFn&lt;/code&gt; is &lt;code&gt;(a, b) =&amp;gt; b - a&lt;/code&gt;. Here comes a classic interview question, will &lt;code&gt;(a, b) =&amp;gt; b - a&lt;/code&gt; be in ascending or descending order? And why?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Will arr be [2, 3, 17, 22, 35, 66] or [66, 35, 22, 17, 3, 2] ?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;(a,b) =&amp;gt; b - a&lt;/code&gt; is ascending or descending? Why?
&lt;/h2&gt;

&lt;p&gt;Will &lt;code&gt;(a, b) =&amp;gt; b - a&lt;/code&gt; be ascending or descending? The answer is &lt;strong&gt;descending&lt;/strong&gt;, so &lt;code&gt;arr&lt;/code&gt; in the above example would become [66, 35, 22, 17, 3, 2] .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [66, 35, 22, 17, 3, 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conversely, if the &lt;code&gt;compareFn&lt;/code&gt; is &lt;code&gt;(a, b) =&amp;gt; a - b&lt;/code&gt;, it will be in &lt;strong&gt;ascending&lt;/strong&gt; order, making &lt;code&gt;arr&lt;/code&gt; become [2, 3, 17, 22, 35, 66].&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 3, 17, 22, 35, 66]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why is this so? Remember we mentioned in the previous paragraph that in &lt;code&gt;compareFn&lt;/code&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;a&lt;/code&gt; is greater than &lt;code&gt;b&lt;/code&gt;, return 1&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;a&lt;/code&gt; is smaller than &lt;code&gt;b&lt;/code&gt;, return -1&lt;/li&gt;
&lt;li&gt;Returns 0 if &lt;code&gt;a&lt;/code&gt; is equal to &lt;code&gt;b&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at the following deduction next:&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;(a, b) =&amp;gt; a - b&lt;/code&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;a &amp;gt; b&lt;/code&gt;, then &lt;code&gt;a - b&lt;/code&gt; will be greater than 0, then return 1, a is placed after b, the order will be b, a, because a is greater than b, so it is &lt;strong&gt;ascending&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;a &amp;lt; b&lt;/code&gt;, then &lt;code&gt;a - b&lt;/code&gt; will be less than 0, then return -1, a is placed before b, the order will be a, b, because a is smaller than b, the result is also &lt;strong&gt;ascending&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, when &lt;code&gt;(a, b) =&amp;gt; b - a&lt;/code&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;a &amp;gt; b&lt;/code&gt;, then &lt;code&gt;b - a&lt;/code&gt; will be less than 0, then return -1, b will be placed after a, the order will be a, b, because a is greater than b, so it is &lt;strong&gt;descending&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;a &amp;lt; b&lt;/code&gt;, then &lt;code&gt;b - a&lt;/code&gt; will be greater than 0, then return 1, b is placed before a, the order will be b, a, because a is less than b, the result is also &lt;strong&gt;descending&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As can be seen from the above example, when &lt;code&gt;(a, b) =&amp;gt; a - b&lt;/code&gt;, no matter a is greater than b, or a is less than b, it will be &lt;strong&gt;ascending&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Conversely, when &lt;code&gt;(a, b) =&amp;gt; b - a&lt;/code&gt;, no matter a is greater than b, or a is less than b, it will be &lt;strong&gt;descending&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you can explain through such an example in the interview, I believe you can pass this interview question.&lt;/p&gt;




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

&lt;p&gt;If you like the content, check out the free frontend interview prep resources my friends and I curated at &lt;a href="https://explainthis.io" rel="noopener noreferrer"&gt;explainthis.io&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I have got frontend engineer offers from both startup and FAANG companies. I would like to help as many people as possible to land a job. If you have any questions regarding to frontend engineering or frontend interview. Feel free to reach out to me at &lt;a href="mailto:hello@explainthis.io"&gt;hello@explainthis.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>motivation</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
