<?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: Dani Schuhman</title>
    <description>The latest articles on DEV Community by Dani Schuhman (@cupofcake92).</description>
    <link>https://dev.to/cupofcake92</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%2F681352%2F118a7f0c-e38e-4452-8bdd-a95f38c3ef07.jpeg</url>
      <title>DEV Community: Dani Schuhman</title>
      <link>https://dev.to/cupofcake92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cupofcake92"/>
    <language>en</language>
    <item>
      <title>JavaScript Error: Parcel Won't Stop Rebuilding in Dev Mode</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Mon, 03 Jan 2022 18:51:16 +0000</pubDate>
      <link>https://dev.to/cupofcake92/error-parcel-wont-stop-rebuilding-in-dev-mode-226</link>
      <guid>https://dev.to/cupofcake92/error-parcel-wont-stop-rebuilding-in-dev-mode-226</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@dav420?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;David Pupaza&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/error?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While working on a little project for a course I'm doing, I was introduced to Parcel, instead of using Webpack as a bundler for front end applications. The bonus of using Parcel, is that it requires zero configuration setup. Once installed properly following the straightforward &lt;a href="https://parceljs.org/getting-started/webapp/" rel="noopener noreferrer"&gt;instructions&lt;/a&gt; on their website, you're instantly up and running without a lot of the headaches that Webpack can cause. &lt;/p&gt;

&lt;p&gt;But when working on this project in development mode, I kept running into a strange error. Parcel should only rebuild and reload a page whenever a file is saved. &lt;/p&gt;

&lt;p&gt;Instead, Parcel continued to rebuild over and over again. Looking in the terminal, I watched as it kept rebuilding multiple times a second, to the point that it lagged the process and eventually crashed. &lt;/p&gt;

&lt;p&gt;Looking around the internet for a solution, I don't really understand why precisely this happens. Some of the discussion as to why the rebuild gets caught in an infinite loop mentioned this happening when running it inside Dropbox, something that I wasn't doing. &lt;/p&gt;

&lt;p&gt;What did work to fix this error, was to delete the &lt;code&gt;.parcel_cache&lt;/code&gt; folder and to restart parcel again. Which seems to work fine, until another build error occurs and throws the rebuild back into an infinite loop. I'm not sure what's causing the error in the cache, but for the time being, this seems to be a moderate workaround so at least getting back to coding is possible. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/parcel-bundler/parcel/issues/3481" rel="noopener noreferrer"&gt;github issue 3481&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Internal Property`[[]]`</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 29 Oct 2021 17:20:57 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-internal-property-1nl2</link>
      <guid>https://dev.to/cupofcake92/javascript-internal-property-1nl2</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@dtopkin1?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Dayne Topkin&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/private?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maybe one day you were playing in the dev browser in Chrome, and one day come across something that seemed a bit different. &lt;/p&gt;

&lt;p&gt;You've printed things out to the console, and something odd appears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo() { 
    console.log("Hello")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo.prototype
{constructor: ƒ}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clicking on the arrow for the constructor, will return an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{constructor: ƒ}
constructor: ƒ foo()
arguments: null
caller: null
length: 0
name: "foo"
prototype: {constructor: ƒ}
[[FunctionLocation]]: VM572:1
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[2]
[[Prototype]]: Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What on earth are those double brackets &lt;code&gt;[[]]&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;It's the internal property. In JavaScript, objects have an internal property known as &lt;code&gt;Prototype&lt;/code&gt;. You can also see that there is a &lt;code&gt;Scopes&lt;/code&gt; inside of these double brackets as well once clicking inside an object. &lt;/p&gt;

&lt;p&gt;Whenever there are &lt;code&gt;[[]]&lt;/code&gt; that appear, it's an internal property that can't be accessed by our code. Both &lt;code&gt;Scopes&lt;/code&gt; and &lt;code&gt;Prototype&lt;/code&gt; are internal properties of the &lt;code&gt;foo&lt;/code&gt; object. &lt;/p&gt;

&lt;p&gt;What's pretty cool, and also very helpful when clicking on the &lt;code&gt;Scopes&lt;/code&gt; internal property, is that when working with some concepts, say, a closure, clicking on the scopes property will show the closure itself.&lt;br&gt;
&lt;/p&gt;

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

const g = function() {
    const a = 23;
    f = function() {
        console.log(a * 2);
    };
};

g();
f();

console.dir(f)

// Returns
ƒ f()
arguments: null
caller: null
length: 0
name: "f"
prototype: {constructor: ƒ}
[[FunctionLocation]]: VM495:3
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clicking on the &lt;code&gt;Scopes&lt;/code&gt; internal property, we can see where the closure lives.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[[Scopes]]: Scopes[3]
0: Closure (g) {a: 23}
1: Script {f: ƒ, g: ƒ}
2: Global {0: Window, window: Window, self: Window, docum...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's pretty cool, isn't it? &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/11003021/what-internal-property-in-ecmascript-is-defined-for" rel="noopener noreferrer"&gt;StackOverFlow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.info/private-protected-properties-methods" rel="noopener noreferrer"&gt;JavaScript Info - Private Protected Properties&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript: Passing by Value vs Pass by Reference</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 22 Oct 2021 17:00:30 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-passing-by-value-vs-pass-by-reference-3pfc</link>
      <guid>https://dev.to/cupofcake92/javascript-passing-by-value-vs-pass-by-reference-3pfc</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@erol?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Erol Ahmed&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/library-cards?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's the big difference between passing Arguments by Reference vs Passing Arguments by Value? &lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive Values
&lt;/h3&gt;

&lt;p&gt;In JavaScript, as with many other languages, at some point, you'll come across this concept. Going back to the call stack and the heap, and where data is stored, &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive" rel="noopener noreferrer"&gt;Primitives&lt;/a&gt; (&lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Undefined&lt;/code&gt;, &lt;code&gt;Null&lt;/code&gt;, &lt;code&gt;Symbol&lt;/code&gt;, &lt;code&gt;BigInt&lt;/code&gt;) are stored in the call stack. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" rel="noopener noreferrer"&gt;Objects&lt;/a&gt; (&lt;code&gt;Object Literal&lt;/code&gt;, &lt;code&gt;Arrays&lt;/code&gt;, &lt;code&gt;Functions&lt;/code&gt;, more...), are stored in the heap. &lt;/p&gt;

&lt;p&gt;When we create a primitive value, JavaScript creates a unique identifier with the value name. It then allocates the memory to an address, with a value. The identifier points to the address, but not the value itself. Primitive values, are immutable. Meaning, even if we might reassign a value, or alter it somewhere else, it does not change everywhere else that the value exists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let season = "Fall";
let nextSeason = season;
season = "Winter";
console.log(season);
console.log(nextSeason);
//// Returns
// Winter 
// Fall
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;season&lt;/code&gt; was reassigned to a value of &lt;code&gt;Winter&lt;/code&gt;, when we mutate it, it does not change the fact that &lt;code&gt;nextSeason&lt;/code&gt; was initially set to &lt;code&gt;Fall&lt;/code&gt;. It remains unchanged. &lt;/p&gt;

&lt;h3&gt;
  
  
  Reference Values
&lt;/h3&gt;

&lt;p&gt;When we create a new object, it's stored in the heap, as the memory address, and then the value itself. When we declare a variable as an object, the identifier created for it, points to a piece of memory in the stack, which in turn points to a piece of memory in the heap where the object is stored. &lt;/p&gt;

&lt;p&gt;Because of this, if multiple copies are created of an object, every time that happens, a new object is not created in memory. It just points to the same identifier for the original object. And so mutating one object has the effect that it changes all objects that point to that same memory address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog = {
    name: "Beau",
    age: 15
};
const puppy = dog; 
puppy.age = "10 months";

console.log('Puppy:', puppy)
// Returns
// Puppy: {name: 'Beau', age: '10 months'}

console.log('Dog:', dog)
// Returns 
// Dog: {name: 'Beau', age: '10 months'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's important to understand the difference between Primitives vs Objects, because otherwise it can lead to some pretty nasty surprises, and bugs in our code. Also, so that when moving into functions, it's possible to understand the difference between Passing by Reference vs. Passing by Value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Passing By Value
&lt;/h2&gt;

&lt;p&gt;Moving into Functions, if we pass a primitive value into a function as an argument, even if a function manipulates that value inside of the context of the function, outside of the function itself, the value will remain as it was. &lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 1;
function alter(arg) {
    arg = 100;
    return arg;
};

change(x)
// Returns 100 
console.log(x)
// returns 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Passing by Reference
&lt;/h2&gt;

&lt;p&gt;When we pass a reference type to the function, what's copied is the reference object to the memory heap. Both point to the same object in memory. When an object is manipulated in a function, it's the same as manipulating the object itself, as both references point to the same object. Change in one place, and everywhere else, the object has been altered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog = {
    name: "Beau",
    age: 15
};

const happyBirthday = function(arg){
     arg.age = arg.age + 1;
} 

happyBirthday(dog)
console.log(dog)
// Returns 
dog {name: 'Beau', age: 16}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;To sum it, when a primitive type is passed into a function, it creates a copy. When we pass an object into a function, it's the same as passing the object itself. Which is something that we should be careful with, as it can again, lead to large issues. &lt;/p&gt;

&lt;h3&gt;
  
  
  Further Reading
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dmitripavlutin.com/value-vs-reference-javascript/#:~:text=In%20JavaScript%2C%20you%20can%20pass,by%20reference%20when%20assigning%20objects." rel="noopener noreferrer"&gt;value vs reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://academind.com/tutorials/reference-vs-primitive-values" rel="noopener noreferrer"&gt;academind&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What are JavaScript Sets?</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 15 Oct 2021 16:00:46 +0000</pubDate>
      <link>https://dev.to/cupofcake92/what-are-javascript-sets-3lpm</link>
      <guid>https://dev.to/cupofcake92/what-are-javascript-sets-3lpm</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@rgaleria?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Ricardo Gomez Angel&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/primary-colors?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript sets
&lt;/h1&gt;

&lt;p&gt;JavaScript sets were introduced in ES6. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set" rel="noopener noreferrer"&gt;&lt;code&gt;Set&lt;/code&gt; objects are collections of values.&lt;/a&gt; There can never be any duplicates inside of them, which can make them pretty useful depending on the situation. &lt;/p&gt;

&lt;p&gt;In order to create a &lt;code&gt;Set&lt;/code&gt;, we use the &lt;code&gt;Set.()&lt;/code&gt; constructor, which will create a new &lt;code&gt;Set&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const colors = new Set([ 'Red', 'Green', 'Blue', 'Purple', 'Purple', 'Red', 'Red', 'Blue', 'Magenta']);

console.log(colors)
// Returns 
Set(5) {'Red', 'Green', 'Blue', 'Purple', 'Magenta'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;Set&lt;/code&gt; can hold mixed data types, just like an array. It is also iterable, like an array. However, there are two big differences between a &lt;code&gt;Set&lt;/code&gt; and an array. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Sets elements are unique. &lt;/li&gt;
&lt;li&gt;The order of elements in a &lt;code&gt;Set&lt;/code&gt; is irrelevant. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When creating a &lt;code&gt;Set&lt;/code&gt;, if we pass in a string as the value, it is an iterable. Whatever is passed in, will become a set of unique characters, with the duplicates removed. Using the string &lt;code&gt;Hello&lt;/code&gt;, if we pass it into a new &lt;code&gt;Set&lt;/code&gt; constructor, the second l will be removed, and we'll be returned a set that consists of only &lt;code&gt;{'H', 'e', 'l', 'o'}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(new Set('Hello'))
// Returns 
Set(4) {'H', 'e', 'l', 'o'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Working with Sets
&lt;/h2&gt;

&lt;p&gt;There are several methods, and properties that can be used with Sets in order to manipulate the data. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.add()&lt;/code&gt; method can add a new element onto our &lt;code&gt;Set&lt;/code&gt;. But just as all values are unique in a set, if you try to add a value twice, the second one will be ignored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.add('Yellow')
Set(6) {'Red', 'Green', 'Blue', 'Purple', 'Magenta', …}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.has()&lt;/code&gt; method can check to see if a set contains a specific element. It's similar to the &lt;code&gt;.includes()&lt;/code&gt; method in arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.has('Yellow')
// Returns 
true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike arrays, as the order doesn't matter in &lt;code&gt;Set&lt;/code&gt;'s, we can't pass in an index. Doing so will return &lt;code&gt;undefined&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(colors[0])
// Returns
undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.size&lt;/code&gt; property is similar to the &lt;code&gt;.length&lt;/code&gt; property in arrays, and will return the size of our &lt;code&gt;Set&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.size
// Returns 
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can delete elements from a set using the &lt;code&gt;.delete()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.delete('Yellow')
// Returns
true 
colors
Set(5) {'Red', 'Green', 'Blue', 'Purple', 'Magenta'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another method that can be used on a &lt;code&gt;Set&lt;/code&gt; is the &lt;code&gt;.clear()&lt;/code&gt; method, which will delete all of the elements of the set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.clear()
colors 
// Returns
Set(0) {size: 0}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also iterate over the items in a set, using both the &lt;code&gt;.forEach()&lt;/code&gt; method, or a for/of loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let item of colors) console.log(item)
// Returns 
Red
Green
Blue
Purple
Magenta
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Converting between a &lt;code&gt;Set&lt;/code&gt; and an &lt;code&gt;Array&lt;/code&gt;.
&lt;/h3&gt;

&lt;p&gt;If we wanted to convert an array into a &lt;code&gt;Set&lt;/code&gt;, it's very simple. We can use the regular &lt;code&gt;Set&lt;/code&gt; constructor on an array, and it will transform it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = ['Hello', 'how', 'are', 'you?']

let newSet = new Set(arr)
console.log(newSet)
// Returns 
Set(4) {'Hello', 'how', 'are', 'you?'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted to convert a &lt;code&gt;Set&lt;/code&gt; into an &lt;code&gt;Array&lt;/code&gt;, we can use the spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log([...newSet])
// Returns 
(4) ['Hello', 'how', 'are', 'you?']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sets also have the &lt;code&gt;keys&lt;/code&gt; and &lt;code&gt;values&lt;/code&gt; methods. &lt;code&gt;keys&lt;/code&gt; is an alias for &lt;code&gt;values&lt;/code&gt;, so both methods do the same thing pretty much. Using either of them will return a new iterator object, that yields the &lt;code&gt;values&lt;/code&gt; for each element in the &lt;code&gt;Set&lt;/code&gt; in the order they appear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let iceCreamFlavors = new Set();
iceCreamFlavors.add('vanilla'); // vanilla
iceCreamFlavors.add('chocolate'); // chocolate
iceCreamFlavors.add('mint chocolate chip'); // mint chocolate chip

let setItr = iceCreamFlavors.values();

console.log(setItr.next().value);
console.log(setItr.next().value);
console.log(setItr.next().value);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Sets are very easy to interact with thanks to how straightforward their methods are, and because of that can be a very useful tool. I hope that this introduction to them has helped explain just how helpful they can be. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Short Circuiting</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 08 Oct 2021 16:34:48 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-short-circuiting-2dij</link>
      <guid>https://dev.to/cupofcake92/javascript-short-circuiting-2dij</guid>
      <description>&lt;h2&gt;
  
  
  What is Short Circuiting in JavaScript?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;||&lt;/code&gt; Operator
&lt;/h3&gt;

&lt;p&gt;When working with logical operators such as AND &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and OR &lt;code&gt;||&lt;/code&gt;, they are typically used with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND" rel="noopener noreferrer"&gt;Boolean values&lt;/a&gt; (truthy and falsy), and return &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. When using the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; Operator, both sides of a conditional must evaluate to &lt;code&gt;true&lt;/code&gt; for it to return true. If one side evaluates to false, then it returns false. When working with the &lt;code&gt;||&lt;/code&gt; Operator, only one side needs to evaluate to &lt;code&gt;true&lt;/code&gt; for it to return &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true || true 
// returns true 

true || false 
// returns true 

false || false 
// returns false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, logical operators can be used with any kind of datatype. If for example, a number and a string were used in a logical operator, rather than return &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, it would return the value itself. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;100 || 'North'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Returns &lt;code&gt;100&lt;/code&gt; rather than &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why does this happen? What does Short Circuiting even mean?
&lt;/h2&gt;

&lt;p&gt;In the above example, short circuiting means essentially that the expression is not evaluated. If the first value is truthy, then it will just return the true value, and stop evaluating. &lt;/p&gt;

&lt;p&gt;If the first value was false however, it continues to evaluate, and then returns the value again with the second expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;null || 'Hello'
// returns 'Hello'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Short Circuiting with the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; Operator
&lt;/h2&gt;

&lt;p&gt;Short circuiting works in the complete opposite manner when it comes to the AND operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true &amp;amp;&amp;amp; true 
// returns true 

true &amp;amp;&amp;amp; false 
// returns false 

false &amp;amp;&amp;amp; false 
// returns false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the AND operator, it short circuits when the first operand is Falsy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O &amp;amp;&amp;amp; 'Fall'
// returns 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A longer example chaining together multiple &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; evaluations works the same. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log('Goodbye' &amp;amp;&amp;amp; 100 &amp;amp;&amp;amp; undefined &amp;amp;&amp;amp; 'halloween')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Will short circuit on &lt;code&gt;undefined&lt;/code&gt; and automatically return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using a practical example, it's possible to take advantage of short circuiting, and to shorten an if statement. &lt;/p&gt;

&lt;p&gt;Rather than writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function a() { 
   console.log('a'); 
   return false; 
}

if (a()) { 
   console.log('Foobar') 
}
// returns a 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can write it as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a() &amp;amp;&amp;amp; console.log('Foobar')
// returns a 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As &lt;code&gt;a()&lt;/code&gt; evaluates to false, it will short circuit, cease executing, and return only a, not continuing on to the other side of the Operator. &lt;/p&gt;

&lt;p&gt;However, it's not necessarily always a good idea to use short circuiting in the place of the longer code of an &lt;code&gt;if&lt;/code&gt; statement, because someone else coming in to read the code, might not understand what's going on. Sometimes it's better to keep things readable, rather than short. Brevity isn't always the answer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;Here is a really great, and long example explaining short circuiting in JavaScript from &lt;a href="https://stackoverflow.com/questions/12554578/does-javascript-have-short-circuit-evaluation" rel="noopener noreferrer"&gt;stack overflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codeburst.io/javascript-what-is-short-circuit-evaluation-ff22b2f5608c" rel="noopener noreferrer"&gt;Codeburst.io JavaScript: What is Short-circuit Evaluation?&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Test-Driven Development?</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 01 Oct 2021 16:09:46 +0000</pubDate>
      <link>https://dev.to/cupofcake92/what-is-test-driven-development-3b0o</link>
      <guid>https://dev.to/cupofcake92/what-is-test-driven-development-3b0o</guid>
      <description>&lt;p&gt;Test-Driven Development, or TDD for short, is a &lt;a href="https://en.wikipedia.org/wiki/Test-driven_development" rel="noopener noreferrer"&gt;software development process&lt;/a&gt; that focuses on writing test cases before you write actual code. &lt;/p&gt;

&lt;p&gt;The idea is that you write a test, the tests fail for obvious reasons, and then you write the simplest code that will then make the code pass, then refactor that code, using the tests to make sure everything is preserved. Then you repeat the process all over again, building more tests and making things more complex. Sounds straightforward enough. &lt;/p&gt;

&lt;h3&gt;
  
  
  So why is it so important?
&lt;/h3&gt;

&lt;p&gt;There are a lot of reasons why TDD is important. A 2005 study showed that programmers who used TDD were more productive. It's also thought to lead to less errors as you are testing as you create. It can lead to more modulated, flexible code, and more optimized code. It can drive the design process of a program. The list goes on and on. &lt;/p&gt;

&lt;p&gt;As a student in the Flatiron School, TDD was used for almost all of the work that we did. I found, personally, that when learning to code, it's much easier to help break down a problem into little pieces and work on things one by one. It's much easier to tackle a task that's made up of twenty-five parts, by chipping away at it bit by bit, rather than trying to handle it all at once. Plus, there is something very satisfying of running tests and watching them change from red to green. &lt;/p&gt;

&lt;p&gt;All of that is very well enough, but how do you go about using TDD if you haven't before? &lt;/p&gt;

&lt;p&gt;There are a lot of wonderful resources on the web, that can talk you through how to set up the frameworks that are used to help drive TDD. What framework you use, depends on what language you use as well. At the moment, I am only familiar with a couple, and can't talk more about how the other ones work and how to specifically write tests for everything mentioned here. But I hope to be able to one day. &lt;/p&gt;

&lt;h3&gt;
  
  
  Here are some popular ones that are commonly used:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://rspec.info/" rel="noopener noreferrer"&gt;RSpec&lt;/a&gt; - Behavior driven development for Ruby. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.pytest.org/en/latest/" rel="noopener noreferrer"&gt;Pytest&lt;/a&gt; - An open source tool used in Python. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://junit.org/junit5/" rel="noopener noreferrer"&gt;Junit 5&lt;/a&gt; - A programmer-friendly testing framework for Java and the JVM. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mochajs.org/" rel="noopener noreferrer"&gt;Mocha&lt;/a&gt; - A feature-rich JavaScript testing framework running on Node.js and in the browser. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; - A JavaScript testing framework that works with projects using Babel, TypeScript, Node, React, Angular, Vue and more. &lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.csunit.org/" rel="noopener noreferrer"&gt;csUnit&lt;/a&gt; - An open source testing tool for the .NET framework. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  But what does TDD look like when implemented if you've never seen a test before?
&lt;/h3&gt;

&lt;p&gt;With a language like &lt;strong&gt;RSpec&lt;/strong&gt;, a test could look like the following, for a User model:&lt;br&gt;
&lt;/p&gt;

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

RSpec.describe User, :type =&amp;gt; :model do 
    let(:user) {
        User.create(
        :email =&amp;gt; "me@mail.com",
        :user_name =&amp;gt; "SomeGuy",
        :password =&amp;gt; "123ABC",
        :id =&amp;gt; 4
        )
    }

    it "is valid with a username, password, and email" do 
        expect(user).to be_valid
    end 

    it "is not valid without a password" do 
        expect(User.new(user_name: "Name")).not_to be_valid
    end 

    it "is not valid without a email" do 
        expect(User.new(password: "Password")).not_to be_valid
    end 

    it "is not valid without a username" do 
        expect(User.new(email: "Boo@mail.com")).not_to be_valid
    end 
end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a language like &lt;strong&gt;pytest&lt;/strong&gt;, it can look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Junit&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import static org.junit.jupiter.api.Assertions.assertEquals;

import example.util.Calculator;

import org.junit.jupiter.api.Test;

class MyFirstJUnitJupiterTests {

    private final Calculator calculator = new Calculator();

    @Test
    void addition() {
        assertEquals(2, calculator.add(1, 1));
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mocha&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Jest&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sum = require('./sum');

test('adds 1 + 2 to equal 3', () =&amp;gt; {
  expect(sum(1, 2)).toBe(3);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;csUnit&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

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

using System.Collections.Generic;

using System.Text;



using csUnit;



namespace Go48.Core {

   [TestFixture]

   public class CalculatorTests {

      [Test]

      public void AddTwoNumbers() {

         // Step 1: Set up some objects

         Calculator calculator = new Calculator();



         // Step 2: Manipulate the objects

         calculator.Enter(3);

         calculator.Enter(5);

         calculator.Add();



         // Step 3: Assert outcome is correct

         Assert.Equals(8, calculator.Top);

      }

   }

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The common thread through all of these tests is that as a programmer, you want something to exist. Maybe it's a function that returns an array, maybe it's a function that performs some kind of mathematic operation, maybe it's creating a User model so that people can log in and out of an application, etc. &lt;/p&gt;

&lt;p&gt;The point is that we want the code to do something, so tests are written first. Then the code is crafted to pass those tests. And then code is refactored, more tests are written, and what started out as a simple operation starts to expand into something more, until something is a full fledged application and hopefully, doesn't have nine-zillion bugs that need to be fixed before it can be used. &lt;/p&gt;

&lt;p&gt;If you've never encountered TDD before, I hope that this brief introduction to the practice explains things enough, that maybe you'll click on the links of the language of your choice, follow a tutorial, and try writing your first test today!&lt;/p&gt;

</description>
      <category>testing</category>
    </item>
    <item>
      <title>Introduction to JavaScript Destructuring</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 24 Sep 2021 18:05:39 +0000</pubDate>
      <link>https://dev.to/cupofcake92/introduction-to-javascript-destructuring-43o8</link>
      <guid>https://dev.to/cupofcake92/introduction-to-javascript-destructuring-43o8</guid>
      <description>&lt;h2&gt;
  
  
  What is JavaScript Destructuring?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="noopener noreferrer"&gt;The destructuring assignment syntax is an ES6 feature, that allows you to unpack values from an array or an object into separate variables.&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Destructuring Arrays
&lt;/h2&gt;

&lt;p&gt;Before destructuring was introduced, if we wanted to extract elements from an array, it would be done as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const seasons = ['Winter', 'Spring', 'Summer', 'Fall'] 

const one = seasons[0];
const two = seasons[1];
const three = seasons[2];
const four = seasons[3];

console.log(one, two, three, four)
// returns
Winter Spring Summer Fall 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But using destructuring, we can do it in a much simpler, and streamlined fashion. &lt;/p&gt;

&lt;p&gt;To use it, start with the &lt;code&gt;const&lt;/code&gt; keyword, followed by brackets &lt;code&gt;[]&lt;/code&gt;. Within the brackets is the destructuring assignment, or the elements we want to abstract out, then set equal to the original array. &lt;/p&gt;

&lt;p&gt;Following that process, in order to destructure the elements of our seasons array, would be done as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [one, two, three, four] = seasons
console.log(one, two, three, four)
// returns 
Winter Spring Summer Fall 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The original array is not mutated, and remains untouched. &lt;/p&gt;

&lt;p&gt;If for whatever reason, we only wanted to pull out certain variables within an array and not all, say only Summer and Fall, to do that within an array, leave an empty space, or a hole.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [,, third, fourth] = ['Winter', 'Spring', 'Summer', 'Fall'] 
console.log(third, fourth)
// returns 
Summer Fall
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also set default values for variables when extracting them, so that if that element is not part of the array, something will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [a, b, c, d, e = "February"] = seasons 
console.log(a, b, c, d, e)
// returns 
Winter Spring Summer Fall February 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is possible to destructure nested arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nestedArr = ['Winter', 'Spring', ['Jan', 'Feb', 'March']]

const [x, , [t, u, v]] = nestedArr;
console.log(x, t, u, v);
// returns 
Winter Jan Feb March
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's also possible to switch the position of variables using destructuring. &lt;/p&gt;

&lt;p&gt;Take the array of flavors, and to test out this example, make sure it is declared with &lt;code&gt;let&lt;/code&gt; and not &lt;code&gt;const&lt;/code&gt;, as using &lt;code&gt;const&lt;/code&gt; will through an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let flavors = ["Vanilla", "Chocolate"];
const [vanilla, chocolate] = flavors; 
console.log(vanilla, chocolate);
// returns 
Vanilla Chocolate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted to switch the flavors in the destructuring assignment, it's possible to do so in one simple line of code, rather than going through the process of reassigning one of the variables to a temporary variable, before reassigning altogether:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [vanilla, chocolate] = [chocolate, vanilla];
console.log(vanilla, chocolate);
// returns 
Chocolate Vanilla 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Destructuring Objects
&lt;/h2&gt;

&lt;p&gt;To use destructuring with objects, the philosophy is pretty much the same, but there are a few differences. The first is that instead of using brackets, curly braces are used instead &lt;code&gt;{}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog = {
    name: "Jack",
    breed: "Heinz 57", 
    age: 10.5,
    likes: [ "Long walks", "Belly rubs", "Chasing Squirrels"]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike with an array, within an object, the order of elements doesn't matter. All we need is the property name to proceed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { name, breed, age, likes } = dog;
console.log(name, breed, age, likes);
// returns 
Jack Heinz 57 10.5 
(3) ['Long walks', 'Belly rubs', 'Chasing Squirrels']
0: "Long walks"
1: "Belly rubs"
2: "Chasing Squirrels"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted the variable names to be different from the property names, we still need to reference the property names as before, but followed by a colon, and the new property name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { name: nickName, breed: type, age: years, likes: interests } = dog;
console.log(nickName, type, years, interests);
// returns 
Jack Heinz 57 10.5 
(3) ['Long walks', 'Belly rubs', 'Chasing Squirrels']
0: "Long walks"
1: "Belly rubs"
2: "Chasing Squirrels"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as with an array, we can also assign a default value within an object. It's done in the same manner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {
  name: nickName,
  breed: type,
  age: years,
  likes: interests,
  favoriteWalk = 'On the street',
} = dog;
console.log(nickName, type, years, interests, favoriteWalk);
// returns 
Jack Heinz 57 10.5 (3) ['Long walks', 'Belly rubs', 'Chasing Squirrels'] On the street
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as it's possible to destructure nested arrays, it's possible to destructure nested objects. &lt;/p&gt;

&lt;p&gt;Again, the curly braces are needed to access an object within an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog2 = {
  name: 'Maya',
  age: 1,
  breed: 'Samoyed',
  address: {
    city: 'Berlin',
    country: 'Germany',
  },
};

const {
  address: { city },
} = dog2;

console.log(city);
// returns 
Berlin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Destructuring is an incredibly powerful, and useful tool for developers. This is just an introduction to some of its capabilities, but there is a lot more that it's possible to do using destructuring assignment in ES6. &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://exploringjs.com/es6/ch_destructuring.html" rel="noopener noreferrer"&gt;Exploring ES6 - Chapter 10: Destructuring&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/" rel="noopener noreferrer"&gt;ES6 in depth&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rails 6: Webpacker::Manifest::MissingEntryError</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Sat, 18 Sep 2021 17:44:53 +0000</pubDate>
      <link>https://dev.to/cupofcake92/rails-6-webpacker-manifest-missingentryerror-4fi7</link>
      <guid>https://dev.to/cupofcake92/rails-6-webpacker-manifest-missingentryerror-4fi7</guid>
      <description>&lt;p&gt;Error handling can either be something that's incredibly aggravating, or satisfying as a developer. Errors inevitably happen, but sometimes even after falling down a google rabbit hole that leads to questions on Stackoverflow, to github threads, can often lead to even more aggravation and not an actual solution. Especially when there is no forward momentum in the errors. At least when an error changes to something new, you know you've done something right. Maybe. &lt;/p&gt;

&lt;p&gt;I'm going to try and walk through a solution to the Rails 6 Webpacker::Manifest::MissingEntryError, in the hope that one person stumbles across this in the future and it helps. &lt;/p&gt;

&lt;p&gt;After setting up a new project in Rails, when attempting to fire up the development server, I was automatically hit with this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Webpacker can't find application.js in /Users/danischuhman/Development/code/photo_app/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the error means is that Webpacker can't find the application, and is failing at square one to compile. None of the files that are needed in order for Webpacker to run properly, are being generated. I don't know precisely why this happens, or where there is a failure to cause this issue, just that it happens. And it's annoying. &lt;/p&gt;

&lt;p&gt;Some of the github threads talked about aliasing a css file, and altering the &lt;code&gt;&amp;lt;%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %&amp;gt;&lt;/code&gt; tag within application.html.erb file. Other workarounds talked about removing the tag entirely. Removing the tag allows everything to run properly, except, it doesn't solve the issue and leads to further errors down the road, which really isn't the best way to handle debugging. Others had solutions that worked for some people, but did not for me. &lt;/p&gt;

&lt;p&gt;The solution, which I only came to after discussing it with a good friend, and much more experienced engineer, is pretty straightforward. &lt;/p&gt;

&lt;p&gt;First thing, is to run &lt;code&gt;rails webpacker:install&lt;/code&gt; and overwrite the existing webpacker.yml file. &lt;/p&gt;

&lt;p&gt;Next is to delete the node_modules folder in your repo, and if it exists, the public/packs folder. In my case, my public/packs folder wasn't even being generated, so there was nothing to delete there. &lt;/p&gt;

&lt;p&gt;Then run &lt;code&gt;yarn install&lt;/code&gt;, &lt;code&gt;bundle&lt;/code&gt;, and then &lt;code&gt;rails webpacker:install&lt;/code&gt;, overwriting any files it suggests you to overwrite. &lt;/p&gt;

&lt;p&gt;Hopefully, if everything has then been installed properly, when you fire up &lt;code&gt;rails s&lt;/code&gt; your application should open to the main page, and there should no longer be an error. Which, I hope if you've found this blog post, is precisely what's happened for you. &lt;/p&gt;

&lt;p&gt;In the future, when building my next Rails App, I will follow this order of operations, just to make sure everything is generated properly and to hopefully avoid the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
rails db:migrate
rails webpacker:install
yarn install
rails server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the time being, I'm glad to have found a solution that works, and hopefully know in the future how to work around it, should the problem arise again. &lt;/p&gt;

&lt;p&gt;Here are some links to discussion of this particular issue, to see just some of the solutions offered up by other engineers. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/webpacker/issues/1494" rel="noopener noreferrer"&gt;github issue 1494&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/webpacker/issues/1523" rel="noopener noreferrer"&gt;github issue 1523&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/59217825/rails-6-server-just-stopped-compiling-webpack" rel="noopener noreferrer"&gt;stackoverflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/59379731/webpackermanifestmissingentryerror-in-localhost3000-errors" rel="noopener noreferrer"&gt;stackoverflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/webpacker/issues/2071" rel="noopener noreferrer"&gt;github issue 2071&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>testdev</category>
    </item>
    <item>
      <title>The JavaScript Call Stack</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 10 Sep 2021 16:52:52 +0000</pubDate>
      <link>https://dev.to/cupofcake92/the-javascript-call-stack-4ec8</link>
      <guid>https://dev.to/cupofcake92/the-javascript-call-stack-4ec8</guid>
      <description>&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@anniespratt?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Annie Spratt&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/stack-of-books?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the JavaScript Call Stack?
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Call_stack" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;: A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.&lt;/p&gt;

&lt;p&gt;At its most basic level, the call stack is where our code is executed, using execution context. &lt;/p&gt;

&lt;p&gt;The JavaScript engine, a program that runs JavaScript code, contains both a call stack and a heap. For now, just know that the heap is a large, unstructured memory pool. &lt;/p&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%2Fpaik76a5zcmo2o58x64l.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%2Fpaik76a5zcmo2o58x64l.png" alt="Visual Representation of the Call Stack Imgur" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is the Call Stack Important?
&lt;/h2&gt;

&lt;p&gt;JavaScript is single threaded, or synchronous. It can only do one thing at a time. We can give it 100 tasks to perform, but it can't perform all 100 tasks simultaneously. It has to go through the process of completing one task before moving onto the next. It's incredibly orderly. &lt;/p&gt;

&lt;p&gt;Within the call stack, say for example, these 100 tasks are all functions. The call stack will stack each function's execution context on top of each other, in the order that they were called, like a pile of books, or boxes, or lego pieces, etc. At the top of the pile, is the current execution context we are within. Once that execution context is finished running, it'll be removed from the top of the pile. The book will be popped off, and it can move down the one directly below. And this will continue happening until the entire stack has been emptied, and the call stack returns to the global execution context, until something new is called. &lt;/p&gt;

&lt;p&gt;But what happens when there is a task within a task, or a second function within another function when in that call stack? In that case, if a second function has been called within a function, a new execution context is created, and pushed onto the top of the stack. The execution of that first function is paused, the second function is run through, and once that is completed, it is popped off the the call stack, and we return to the execution context of the first function, which then continues until it's finished, and we return to the Global Execution Context. &lt;/p&gt;

&lt;p&gt;While it might seem a little silly, here is an example of how switching execution contexts, and moving through the call stack works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function first(){
    console.log("Inside First.")
    function second(){
        console.log("First execution context paused, now inside Second.")
        function third(){
            console.log("Second execution context paused, now inside Third.")
        }
      third();
      console.log("Return to second's execution context."); 
    } 
  second();
  console.log("Return to first's execution context.");
}

first()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When first is called, it will print out to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inside First.
First execution context paused, now inside Second.
Second execution context paused, now inside Third.
Return into second's execution context.
Return to first's execution context.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time a new function was called, the call stack worked its way through the execution context until completed, before moving back to the previous function, until eventually, all execution contexts were finished, and we returned to the Global Execution Context. &lt;/p&gt;

&lt;p&gt;The Global execution context will only pop off of the call stack when our program finishes, either by closing a browser window, or closing the terminal. &lt;/p&gt;

&lt;h3&gt;
  
  
  Further Reading:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://eloquentjavascript.net/2nd_edition/03_functions.html" rel="noopener noreferrer"&gt;Eloquent JavaScript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop" rel="noopener noreferrer"&gt;MDN Concurrency Model and the Event Loop&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Scope</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 03 Sep 2021 17:42:35 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-scope-5306</link>
      <guid>https://dev.to/cupofcake92/javascript-scope-5306</guid>
      <description>&lt;h2&gt;
  
  
  What is Scope?
&lt;/h2&gt;

&lt;p&gt;In short, Scope is context. It's where something is available. In JavaScript, it has to do with where declared variables are accessible, how they are organized and accessed by the JavaScript Engine. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Three Different Types of Scope in JavaScript
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Global Scope
&lt;/h3&gt;

&lt;p&gt;This is the top of the food chain, so to speak. These are variables that are declared outside of any functions, or block. Variables declared in global scope are accessible everywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const favoriteColor = "Blue";
let meal = "Dinner";
var year = 2021;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Scope or Local Scope
&lt;/h3&gt;

&lt;p&gt;Variables are accessible &lt;strong&gt;only&lt;/strong&gt; inside of a function. They are not available outside of the function. When you try to call a variable that has been defined inside of a function, outside, you'll get a ReferenceError. This is referred to either as function scope, or local scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function pets(){
   const animal = "Kitten";
   const name = "Fluffy";
   console.log(`This is my ${animal}, their name is ${name}`);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Block Scope (ES6)
&lt;/h3&gt;

&lt;p&gt;Starting in ES6, variables are only accessible inside of the block, and block scoped. However, this only applies to variables declared with &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;. It does not apply to those declared with &lt;code&gt;var&lt;/code&gt;. Functions are also block scoped, but only when using strict mode in your code. &lt;/p&gt;

&lt;p&gt;A block of code, is usually anything created between the curly braces {}. Typically they are if/else statements, loops, and switch statements. If a variable is created using &lt;code&gt;var&lt;/code&gt; within a block, it's accessible outside of that block, scoped to the current function, or to the global scope. &lt;/p&gt;

&lt;p&gt;This also means that any functions defined with a block of code, are only accessible in that block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
   const month = "September";
   let day = "Friday";
   const year = 2021;

   console.log(month, day, year)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lexical Scope
&lt;/h3&gt;

&lt;p&gt;Lexical Scope means that any children have access to variables defined in the parent scope. The way variables are organized and accessed is controlled by the placements of functions and blocks within the code. It is lexically bound to the execution context of its parents scope. &lt;/p&gt;

&lt;p&gt;However, it is incredibly important to note, that the way that lexical scope works, is only up. A child can access variables available in its parents scope, but a parent cannot access downwards. Whatever is defined within its children, is not accessible within the parents context. This is called the scope chain, and it is a one-way street essentially, anything on the outside does not have access to what is on the inside.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function food(){
   const fruit1 = "banana";
   console.log(fruit1);
   function food2() {
     const fruit2 = "apple";
     console.log(fruit1, fruit2);
      function food3() {
         const fruit3 = "orange";
         console.log(fruit1, fruit2, fruit3);
      }
     food3();
   }
  food2();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call &lt;code&gt;food()&lt;/code&gt; in the console, it will print out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;banana
banana apple 
banana apple orange
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we tried to call &lt;code&gt;food2()&lt;/code&gt; and &lt;code&gt;food3()&lt;/code&gt; directly in the console, we get Uncaught ReferenceError, neither function is defined. They are not available within the parents scope. &lt;/p&gt;

&lt;p&gt;This happens because of variable lookup in the scope chain. If one scope needs to find a certain variable, such as &lt;code&gt;fruit1&lt;/code&gt;, or &lt;code&gt;fruit2&lt;/code&gt;, within the scope of the &lt;code&gt;food3()&lt;/code&gt; function, it will lookup to the parents scope to find it. In the case of the variable &lt;code&gt;fruit2&lt;/code&gt;, looking up one scope to the &lt;code&gt;food2()&lt;/code&gt; function, &lt;code&gt;fruit2&lt;/code&gt; is found, and then used. But as &lt;code&gt;fruit1&lt;/code&gt; has not been found, it looks up another scope, to that of the &lt;code&gt;food()&lt;/code&gt; function, where it is found, and then used. It will continue to lookup the chain all the way to the global scope, and if during that variable lookup, no variable is found, an error will be presented. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Functions</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Thu, 26 Aug 2021 19:05:30 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-functions-53ei</link>
      <guid>https://dev.to/cupofcake92/javascript-functions-53ei</guid>
      <description>&lt;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;What are JavaScript functions? They are one of the most fundamental building blocks of JavaScript. At its most basic level, functions are objects. An object that contains some code that we would like to call, or execute multiple times. &lt;/p&gt;

&lt;p&gt;The purpose of functions, is to put reusable code inside of a function block, that we can then use at different points, rather than having to write everything out over and over, and violating the principle of Don't Repeat Yourself. &lt;/p&gt;

&lt;h2&gt;
  
  
  Functions Syntax
&lt;/h2&gt;

&lt;p&gt;Functions are made up of several key parts. &lt;/p&gt;

&lt;p&gt;We define a function using the keyword &lt;code&gt;function&lt;/code&gt;, and then a function name, followed by parentheses and then curly braces &lt;code&gt;{}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Within the curly braces, is the function body, or the code that will execute when we run the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo(){
    console.log("I was called!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the function has been written, we can use it as many times as we want. We call, invoke, or run the function, by writing the function name, followed by parentheses. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;foo();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And we can call a function as many times as we wish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo();
foo();
foo();
foo();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which would then output to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I was called!
I was called!
I was called!
I was called!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically, passed in between the parentheses will be parameters, or arguments, separated by a comma. And if we want to return some kind of data from the function, we can do so using the &lt;code&gt;return&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(argument1, argument2){
   return argument1 + argument2; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If parameters are used, they act like placeholders, placeholders for the actual data we wish to pass in, and be executed. So that our add function can be called like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add(1, 100);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Which will then return &lt;code&gt;101&lt;/code&gt; to the console. &lt;/p&gt;

&lt;p&gt;But if we wrote the function incorrectly, and did not use the return keyword, instead writing it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(argument1, argument2){
   argument1 + argument2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling our function with arguments, &lt;code&gt;add(1, 100)&lt;/code&gt; would result in an &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Where it starts to get a little tricky, or perhaps confusing, is that functions aren't always so straightforward. Not all functions have to have arguments. Not all functions return something. &lt;/p&gt;

&lt;p&gt;There are also different ways of writing functions as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Declarations vs Function Expressions
&lt;/h2&gt;

&lt;p&gt;The type of function we have learned about thus far, is called a function declaration. A &lt;strong&gt;function declaration&lt;/strong&gt; is a function created using the function keyword. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function" rel="noopener noreferrer"&gt;Function Declarations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;function expression&lt;/strong&gt;, is a function written without a name, but stored within a variable. We can also call these nameless functions, &lt;strong&gt;anonymous functions&lt;/strong&gt;. They work the same way that function declarations work, as functions are essentially just values in JavaScript. &lt;/p&gt;

&lt;p&gt;If we wanted to rewrite our add function as a function expression, we could do so like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = function(arg1, arg1) { 
   return arg1 + arg2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function can be called the same way, although we are calling the variable name add rather than the function name, and supplying it with two arguments. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;add(100, 1)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Which will return &lt;code&gt;101&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The biggest difference between a function declaration, and a function expression, is that if you call a function declaration before it's been defined, you'll still get the same result. If you call a function expression before it's been defined, you'll get an error as a function expression cannot be called before it's been initialized. This is all do to a concept called hoisting, which I'm not going to go into today. &lt;/p&gt;

&lt;h2&gt;
  
  
  Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Arrow functions, or an arrow function expression, is a shorter syntax for writing function expressions essentially, although it can't be used in all situations. They are always anonymous, as they are not created using a function name. &lt;/p&gt;

&lt;p&gt;Arrow functions appear very much like function expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (arg1, arg2) =&amp;gt; arg1 + arg2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One difference is that you can omit the &lt;code&gt;return&lt;/code&gt; keyword. Because of how an arrow function is written, it implicitly returns the result of our last expression immediately. &lt;/p&gt;

&lt;p&gt;Another is that if there is only one argument being passed in, we can omit the parentheses altogether.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const squared = x =&amp;gt; x * 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as function expressions do not have to have a parameter, so too can arrow functions not have one. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope that this has helped clarify some of the different types of functions that can be used when working in JavaScript. I haven't covered all types of functions at this point in time.&lt;/p&gt;

&lt;p&gt;There is another type of function in JavaScript, called an Immediately Invoked Function Expression, or an &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE" rel="noopener noreferrer"&gt;IFFE&lt;/a&gt;, understanding IFFE's relies upon a deeper understanding of Scope in JavaScript as well as hoisting.&lt;/p&gt;

&lt;p&gt;There are also generator functions as well, you can read more about them &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/js/js_functions.asp" rel="noopener noreferrer"&gt;W3Schools&lt;/a&gt;&lt;br&gt;
&lt;a href="https://eloquentjavascript.net/03_functions.html" rel="noopener noreferrer"&gt;Eloquent JavaScript&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Events</title>
      <dc:creator>Dani Schuhman</dc:creator>
      <pubDate>Fri, 20 Aug 2021 17:27:09 +0000</pubDate>
      <link>https://dev.to/cupofcake92/javascript-events-2fa8</link>
      <guid>https://dev.to/cupofcake92/javascript-events-2fa8</guid>
      <description>&lt;p&gt;What is a JavaScript Event? &lt;/p&gt;

&lt;p&gt;JavaScript has the ability to listen and react to what is happening in the browser. &lt;a href="https://www.w3schools.com/js/js_events.asp" rel="noopener noreferrer"&gt;Events are essentially "things" that happen on the page.&lt;/a&gt;. Things that JavaScript then reacts to. When you say, click the button to like this post, use your cursor to scroll down in order to see more text, write a comment, log into a page, all of these things happen because code has been written to "listen" to such a thing happen, so that the browser then responds appropriately. &lt;/p&gt;

&lt;p&gt;There are all sorts of types of events that you can write code to listen for. Going over to the MDN documentation, there is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Events" rel="noopener noreferrer"&gt;complete list.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Types of User Events
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mouse Events
&lt;/h3&gt;

&lt;p&gt;These are things that happen when you interact with the DOM with a mouse or trackpad. This can happen with what are called click events, when you click, double-click, right-click, left-click, etc on a page. There is also &lt;code&gt;mouseup&lt;/code&gt;, &lt;code&gt;mousedown&lt;/code&gt;, &lt;code&gt;mouseover&lt;/code&gt;, &lt;code&gt;mouseout&lt;/code&gt;, etc. &lt;/p&gt;

&lt;h3&gt;
  
  
  Keypress
&lt;/h3&gt;

&lt;p&gt;These are things that happen when you press down on the keyboard, we can listen for &lt;code&gt;keyup&lt;/code&gt;, &lt;code&gt;keydown&lt;/code&gt;, and &lt;code&gt;keypress&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Form Events
&lt;/h3&gt;

&lt;p&gt;Often you'll see form events consist of submitting a form in HTML. Say, when you sign up for a website and need to fill in information to register as a new user, the browser has been configured to listen to that submit event (the submit button typically), and then sends the information you've just filled in, to a server and does something with it. Other form events include &lt;code&gt;change&lt;/code&gt; and &lt;code&gt;input&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;All of this is pretty interesting, but, what do we do with this information? How do you "listen" for all sorts of events to happen. &lt;/p&gt;

&lt;p&gt;Enter event handlers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Event Handlers
&lt;/h2&gt;

&lt;p&gt;An event handler is a way to add an event listener onto the page. It's a function, that we pass in arguments to listen to our code. &lt;/p&gt;

&lt;p&gt;In order to tell our code how to respond, we use a function called &lt;code&gt;addEventListener()&lt;/code&gt;. This allows us to associate a specific event with a specific response. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;addEventListener()&lt;/code&gt; takes two arguments. The 'event' we are listening to, say, a click, and then a callback function that will then execute that event. &lt;/p&gt;

&lt;p&gt;A simple example with pseudocode would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;element.addEventListener('click', function(/* do some stuff here when we click on the page */)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Trying out the example in real life, say we wanted to create an event when clicking on the header image of this article. The first piece of information we need to do that, is the class of that particular element. Right clicking inspect upon the image element, will reveal the information in the DOM. &lt;/p&gt;

&lt;p&gt;The image has a class of &lt;code&gt;"#main-title &amp;gt; div.crayons-article__cover &amp;gt; img")&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Dropping into the dev tools JavaScript console, we need to first set the element we are adding an event to, to a variable. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const element = document.querySelector("#main-title &amp;gt; div.crayons-article__cover &amp;gt; img")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we need to add an event listener onto that particular element following the pseudocode example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;element.addEventListener('click', function(){
   alert("I was clicked!")
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying that out in the console, when you click on the main image, you should see an alert window pop up, showing our message.&lt;/p&gt;

&lt;p&gt;Congratulations, you've just created your first JavaScript event. &lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Reading
&lt;/h2&gt;

&lt;p&gt;Eloquent JavaScript has a very thorough and in depth explanation of this topic. You can read more &lt;a href="https://eloquentjavascript.net/15_event.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're at all confused about the difference between an event handler or listener, &lt;a href="https://stackoverflow.com/questions/6929528/whats-the-difference-between-event-handlers-listener" rel="noopener noreferrer"&gt;StackExchange&lt;/a&gt; has a good breakdown of what precisely happens on the page.  &lt;/p&gt;

&lt;p&gt;MDN, also has a very good breakdown, walking you through JavaScript events in much more detail. &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;

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