<?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: Zachary Dinerstein</title>
    <description>The latest articles on DEV Community by Zachary Dinerstein (@zacharydinerstein).</description>
    <link>https://dev.to/zacharydinerstein</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%2F394768%2Fe3d44390-168e-4c95-aa73-d4073d957255.jpeg</url>
      <title>DEV Community: Zachary Dinerstein</title>
      <link>https://dev.to/zacharydinerstein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zacharydinerstein"/>
    <language>en</language>
    <item>
      <title>Drawn to coding: JS functions, pt 1</title>
      <dc:creator>Zachary Dinerstein</dc:creator>
      <pubDate>Mon, 26 Jul 2021 19:23:00 +0000</pubDate>
      <link>https://dev.to/zacharydinerstein/understanding-js-functions-pt-1-getting-started-3m8d</link>
      <guid>https://dev.to/zacharydinerstein/understanding-js-functions-pt-1-getting-started-3m8d</guid>
      <description>&lt;p&gt;Functions are maybe the most useful tool in your coding toolkit. Well-written functions can turn horrible spaghetti code into logical programs that are easier to understand and maintain.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll explain what JavaScript functions are, why they're important, and how you can put them to good use. Let's dive in! 🤿&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Who is this tutorial for?&lt;/strong&gt;&lt;br&gt;
Everything here is for total beginners – you don't need to know anything about programming, web development or JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
  Some tech terms that might be new
  &lt;br&gt;
&lt;strong&gt;Console&lt;/strong&gt;&lt;br&gt;
The console is part of your browser. It's used to log errors and debug issues with your code. If there's problem with your internet connection for instance, you'll probably see an error message pop up in the console. 

&lt;p&gt;In this tutorial, we'll make sure our code is working by logging messages there. &lt;/p&gt;

&lt;p&gt;If you're using Chrome, you can open the console by pressing Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux). &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%2Flwoequbfb5krq1644qqq.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%2Flwoequbfb5krq1644qqq.png" alt="Example of Chrome's Console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more, &lt;a href="https://www.youtube.com/watch?v=q9jAFZjPFHo" rel="noopener noreferrer"&gt;check out this video&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser&lt;/strong&gt;&lt;br&gt;
A browser is a program your computer uses to open a webpage (you probably know this because you're using one right now to read this sentence). Examples of browsers are Chrome, Firefox and Safari to name a few. &lt;/p&gt;



&lt;br&gt;

  How to code along with this tutorial
  &lt;p&gt;There's an empty CodeSandbox interface at the bottom of this article you can use to practice along with each exercise. &lt;/p&gt;

&lt;p&gt;You can also create your own CodeSandbox on their website if you want to save your program and reopen it later. &lt;a href="https://codesandbox.io/s/" rel="noopener noreferrer"&gt;Checkout CodeSandbox here&lt;/a&gt;.&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%2Fcbekyvj2bk7y0i1lf1dm.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%2Fcbekyvj2bk7y0i1lf1dm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once your CodeSandbox is set up, delete everything in index.js to start with a clean slate. &lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Functions?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;&lt;br&gt;
Think of functions as little factories. You turn them on and they spit out a desired result on command. &lt;/p&gt;
&lt;/blockquote&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%2Fasxtcbzkzuou167ejzle.jpg" 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%2Fasxtcbzkzuou167ejzle.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say you want to write a program that logs your name and address to the console.&lt;/p&gt;

&lt;p&gt;You could write something like this (open 'console' below to see output):&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/suspicious-snowflake-0zs9l?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This works fine, but let's say you want to log your name and address three times. How would you do this? &lt;/p&gt;

&lt;p&gt;You could copy and paste that code over and over, but that's pretty repetitive. And the first rule of programming is DRY (Don't Repeat Yourself).&lt;/p&gt;

&lt;p&gt;Instead, let's write (you guessed it) a function!&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/nostalgic-yonath-hepny?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Much better! When this function's called, it will execute everything between those curly brackets – what we call the &lt;em&gt;body&lt;/em&gt; of our function. &lt;/p&gt;

&lt;p&gt;But as you can see, we haven't logged anything to our console. Why is that?&lt;/p&gt;

&lt;p&gt;We didn't log anything because we didn't actually &lt;em&gt;call&lt;/em&gt; our function.&lt;/p&gt;

&lt;p&gt;To call (also known as &lt;em&gt;invoke&lt;/em&gt;) a function, you write the function's name and add parentheses to the end, 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;logName();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/pensive-margulis-k0gms?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;BOOM. We just wrote AND called our first function. Give yourself a pat on the back.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;&lt;br&gt;
Think of the parens on the end of the function name like a red ball on the end of a lever. In order to start our factory (&lt;em&gt;call&lt;/em&gt; our function), we need to PULL on that giant lever. &lt;br&gt;
Adding the parens to the end of the function name is how we pull that lever and start our factory running. &lt;/p&gt;
&lt;/blockquote&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%2F41l0u76d9sg0v06xofdt.jpg" 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%2F41l0u76d9sg0v06xofdt.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can call that function as many times as we want, logging multiple strings to the console. ('Strings' are what text is called in JavaScript.) This saves us from writing tons of code. Let's try. &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/sleepy-brattain-2fqk3?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Much nicer. See how this code is easier to read? We're not cluttering up our program by pasting gobs of console logs over and over.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;br&gt;
When you write function or variable names, capitalization counts. logName is not the same as LogName. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Parts of a function&lt;/strong&gt;&lt;br&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%2Fzte9kburekw5dqvprhe0.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%2Fzte9kburekw5dqvprhe0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Making Your Functions More Flexible
&lt;/h2&gt;

&lt;p&gt;Our function's off to a great start, but it's a little rigid. It only does one thing: log a specific name and address. &lt;/p&gt;

&lt;p&gt;Coming back to our factory analogy, let's say you built a factory that produces red Honda sedans. If you wanted to switch it up and produce &lt;em&gt;blue&lt;/em&gt; Honda sedans one day of the week, what would you do? Build an entirely new factory that manufactures Hondas, only in blue?&lt;/p&gt;

&lt;p&gt;You could, but that's a giant waste of resources and time. &lt;/p&gt;

&lt;p&gt;Instead, being the industrious entrepreneur you are, you'd probably set up your factory to produce a different colored car based on whatever color you loaded into it that day. This approach is MUCH better, because it makes your factory flexible. If you want yellow cars, you load in yellow. If you want purple cars, you load in purple, and on and on.&lt;/p&gt;

&lt;p&gt;We want our functions to be this flexible. &lt;/p&gt;

&lt;p&gt;Here's how we might redesign our code to print &lt;em&gt;any&lt;/em&gt; name to the console, not just 'The Dude'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logName(name){
     console.log(name);
     console.log('5 Slacker Ave')
     console.log('Los Angeles, CA 10001')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We made two changes to logName. First, we added a parameter inside the parentheses. Second, we included that same parameter inside the function's body (the code between the brackets).&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%2F93nnhgumg7sgbv2eodqv.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%2F93nnhgumg7sgbv2eodqv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These changes look small, but they're powerful.&lt;/p&gt;

&lt;p&gt;This allows us to pass any string into the function, and then use that string inside the function. &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;logName('Donny');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Donny
// 5 Slacker Ave
// Los Angeles, CA 10001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Amazing! Now our function can log whatever we pass it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;&lt;br&gt;
The parentheses in a function declaration act as a kind of loading dock for our factory. Anything we load in through those parentheses we can then use inside our factory. &lt;/p&gt;
&lt;/blockquote&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%2Fnw9gl8lj9u9npe8aowki.jpg" 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%2Fnw9gl8lj9u9npe8aowki.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;&lt;br&gt;
We can name our parameters whatever we want. If we want to use that parameter in the function's body however, we need to match that name &lt;em&gt;exactly&lt;/em&gt;. Just like with our function names, spelling and capitalization count. &lt;/p&gt;
&lt;/blockquote&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 logName(orange){
     console.log(orange);
     console.log('5 Slacker Ave');
     console.log('Los Angeles, CA 10001');
}

logName('Walter');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works just fine.&lt;/p&gt;

&lt;p&gt;The examples below however will log 'Undefined' or send an error to the console:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/sweet-bartik-437sh?view=split"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/hidden-resonance-tojnp?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What Else Can We Do With Parameters?
&lt;/h2&gt;

&lt;p&gt;We're not limited to just one – we can add as many parameters as we want to our functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function logAddress(name, street, cityStateZip){
     console.log(name);
     console.log(street);
     console.log(cityStateZip);
}

logAddress('Walter', '10 Aggression Ave', 'Los Angeles, CA 10001')

// Walter 
// 10 Aggression Ave
// Los Angeles, CA 10001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just make sure each parameter is separated by a comma, that the arguments are also separated by commas, and that they're in matching order. The first argument corresponds to the first parameter, the second corresponds to the second, and so on…&lt;/p&gt;

&lt;p&gt;("Arguments" are what we call parameters when they appears inside a function call.)&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%2Fbrjpjbimh7hj5crzjv6f.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%2Fbrjpjbimh7hj5crzjv6f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now It's Your Turn!
&lt;/h2&gt;

&lt;p&gt;Use the CodeSandbox below to test what you've learned.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Write a function that logs the name of your first pet, type of animal and their favorite toy. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update the function so it uses parameters and arguments&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/exciting-noyce-kmqq6?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;
  Our Solution
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Question 1
function logPet() {
  console.log("Mac");
  console.log("Dog");
  console.log("Bone");
}

// Question 2
function logPet2(name, animal, toy) {
  console.log(name);
  console.log(animal);
  console.log(toy);
}

logPet();
// Mac
// Dog
// Bone

logPet2("Raptor", "Iguana", "Warm rock");
// Raptor
// Iguana
// Warm rock

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

&lt;/div&gt;




&lt;/p&gt;

&lt;p&gt;That's it for now! In part 2 – coming soon – we'll learn about return statements, arrow functions and other JS function stuff. If you liked this tutorial, please let me know, and feel free to include requests of topics you want me to cover in the future. Happy coding!&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%2Fu4dgmu0vm7wxcdpe8ibd.jpg" 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%2Fu4dgmu0vm7wxcdpe8ibd.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

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