<?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: Lauren Slayman</title>
    <description>The latest articles on DEV Community by Lauren Slayman (@lslayman).</description>
    <link>https://dev.to/lslayman</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%2F1060923%2Fa07e31f5-2067-4110-be6b-cf7dc6820c56.jpg</url>
      <title>DEV Community: Lauren Slayman</title>
      <link>https://dev.to/lslayman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lslayman"/>
    <language>en</language>
    <item>
      <title>Manually Debugging Common Flask Errors</title>
      <dc:creator>Lauren Slayman</dc:creator>
      <pubDate>Sun, 11 Jun 2023 23:29:56 +0000</pubDate>
      <link>https://dev.to/lslayman/manually-debugging-common-flask-errors-44g0</link>
      <guid>https://dev.to/lslayman/manually-debugging-common-flask-errors-44g0</guid>
      <description>&lt;p&gt;When it comes to testing and debugging while building Flask applications, there are luckily a ton of tools and frameworks that can be utilized — some are even already built-in! However, as many programmers already know, it can be far too easy to rely solely on these methods, leaving one in the weeds when encountering errors that feel all but impossible to decode. In fact, there are a several reasons why knowing how to debug manually in Flask is not only an important skill but a truly invaluable habit to maintain; here are a few examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It provides developers far more &lt;strong&gt;flexibility and control&lt;/strong&gt; over the debugging process. You’re able to zone in more closely on the root cause of the issue and tailor your techniques (i.e. print statements, logs, or breakpoints) to fit the situation and/or your particular goals.&lt;/li&gt;
&lt;li&gt;There are few methods for &lt;strong&gt;deepening your understanding&lt;/strong&gt; of your codebase and its execution flow that are more valuable than being able to break down your errors into bite-size pieces and truly unravel the source of the bugs. As irritating as it can be to spend portions of your worktime untangling errors instead of actively building, when you’re finally able to move past the error thrown your way with a more concrete understanding of what led to the error and/or how intimately it affected other portions of your code, your time was still genuinely well spent!&lt;/li&gt;
&lt;li&gt;Because it doesn’t require any additional tools or frameworks and it can, manually debugging is usually &lt;strong&gt;non-invasive&lt;/strong&gt;. Further, it can be completely directly within the codebase without the added weight of external dependencies.&lt;/li&gt;
&lt;li&gt;In addition to simply being a good habit, several manual debugging techniques can applied across other projects and frameworks, making it an extremely &lt;strong&gt;portable skill&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;While the process of debugging can often feel time consuming, manual debugging is exceedingly &lt;strong&gt;quick and simple&lt;/strong&gt; — it doesn’t rely on extensive configuration nor setup time for additional debugging tools or libraries. This is particularly beneficial when working on projects requiring a quick turnaround.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, there are obviously countless situations in which debugging tools, frameworks, or integrated development environments (IDEs, for short) would be ideal — they can provide advanced features, automated breakpoints, and real-time inspections that can make the debugging process feel simpler and more efficient, especially when dealing with large codebases, so the point of this essay isn’t to devalue common debugging tools and frameworks, simply to emphasize why knowing how to (and frequently practicing) manually debug errors is such an important skill and habit. &lt;/p&gt;

&lt;p&gt;To continue, here are some common Flask errors and how to debug them, step-by-step:&lt;/p&gt;

&lt;p&gt;First, here’s a code snippet of a simple Flask application with a single route defined for the root URL &lt;code&gt;('/')&lt;/code&gt; and the route handler will return a simple greeting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to the home page!'

if __name__ == '__main__':
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s see what happens when we try to expand upon it.&lt;/p&gt;

&lt;h3&gt;
  
  
  404 Not Found
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What it means: The requested route or URL does not exist within the Flask application. See the example below:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to the home page!'

@app.route('/about')
def about():
    return 'This is the about page.'

if __name__ == '__main__':
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we wrote in a new route, &lt;code&gt;'/about'&lt;/code&gt;, but there is not a corresponding route handler, so if you were to try to access the URL in a tool like cURL or Postman, you’d likely encounter the following console error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;404 Not Found: The requested URL was not found on the server. If you entered the URL manually, please check your spelling and try again.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;First, review the code and ensure that the correct route has been defined&lt;/li&gt;
&lt;li&gt;Then, check for any typos or syntax errors within the route definition&lt;/li&gt;
&lt;li&gt;Next, verify that the Flask server has been restarted after any code changes have been made&lt;/li&gt;
&lt;li&gt;If confident that the code is correct, double-check the URL to ensure it matches the precise route definition&lt;/li&gt;
&lt;li&gt;If the issue persists, you could add print statements and/or logging message in the route handlers (this will trace the execution flow and ensure that the handler function is being called), like such:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/about')
def about():
    print('Inside the about route handler')
    return 'This is the about page.'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The console output will help you determine if the route handler is being reached or if the issue may lie elsewhere.&lt;/p&gt;

&lt;p&gt;Further, you could set the Flask debugger setting to &lt;code&gt;'debug=True'&lt;/code&gt; and intentionally trigger an exception. For instance, the following line could be added inside the ‘about’ route handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;raise Exception('Testing 404 error handling')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  405 Method Not Allowed
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What it means: The requested HTTP method if not allowed for the specified route in the Flask application
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to the home page!'

@app.route('/about', methods=['GET'])
def about():
    return 'This is the about page.'

if __name__ == '__main__':
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll notice that the route for the &lt;code&gt;'/about'&lt;/code&gt; page above, is only configured for ‘GET’ requests within the ‘methods’ parameter. Let’s assume you were trying to make a ‘POST’ request, you’d come up against the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;405 Method Not Allowed: The method is not allowed for the requested URL.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To troubleshoot, you’d first want to double check that the allowed methods specified within the route decorator’s ‘methods’ parameter are accurate within the code&lt;/li&gt;
&lt;li&gt;Then, double check that the HTTP method used in the cURL or Postman request matches the allowed methods written within the code

&lt;ul&gt;
&lt;li&gt;Likewise, ensure that the code wasn’t intentionally restricted to specific HTTP methods and if it was, that the methods match the intended operation&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;If the issue persists, add print statements or logging messages in the route handlers, as specified with the 404 error above:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/about', methods=['GET'])
def about():
    print('Inside the about route handler')
    return 'This is the about page.'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Additionally, you can inspect the request object (&lt;code&gt;'request.method'&lt;/code&gt;) within the route handler to verify the actual HTTP method being used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  500 Internal Server Error
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What it means: An unhandled exception or error has occurred during the execution of the Flask application
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome to the home page!'

@app.route('/about')
def about():
    # Simulating a server error by accessing an undefined variable
    result = 10 / 0
    return 'This is the about page.'

if __name__ == '__main__':
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Admittedly, the snippet above will automatically induce a server error in the &lt;code&gt;'/about'&lt;/code&gt; route by dividing a number by zero. However, once again by using cURL or Postman, we’ll try to debug this error, step-by-step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, ensure you’re trying to access the correct route within your HTTP processing tool. If incorrect, you’re likely to encounter the following error, meaning an unhandled exception occurred in the execution process:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;500 Internal Server Error: Internal Server Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The console output should also display a traceback with more detailed information about the error.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ZeroDivisionError: division by zero
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This error can be resolved by removing or resolving the problematic line. For this specific case, it can can replaced with a valid calculation or simply removed altogether:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This error can be resolved by removing or resolving the problematic line. For this specific case, it can can replaced with a valid calculation or simply removed altogether:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If the traceback doesn’t help you identify the error, consider adding print statements with the handler, as seen in the earlier error examples.&lt;/li&gt;
&lt;li&gt;Finally, restart the Flask server and retry accessing the route-in-question to confirm whether the error has been resolved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Admittedly, we’ve only begun to scratch the surface of errors one could encounter when writing and troubleshooting code in Flask, but I’m hopeful this guide will aid other novice programmers in troubleshooting some the most commonly encountered errors within Flask, as it’s certainly a habit I’ve been trying to better ingrain within my own code-writing.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>flask</category>
    </item>
    <item>
      <title>Python &amp; SQL: A Match Made in Data Manipulation Heaven</title>
      <dc:creator>Lauren Slayman</dc:creator>
      <pubDate>Fri, 19 May 2023 23:51:02 +0000</pubDate>
      <link>https://dev.to/lslayman/python-sql-a-match-made-in-data-manipulation-heaven-16cj</link>
      <guid>https://dev.to/lslayman/python-sql-a-match-made-in-data-manipulation-heaven-16cj</guid>
      <description>&lt;p&gt;There are several reasons that Python’s popularity has taken off in recent years: it’s simple, readable, and exceptionally versatile. While it wasn’t necessarily created with data management in mind, its extensive library and colossal ecosystem of third-party packages and frameworks have made it an invaluable asset when it comes to data analysis, manipulation and visualization.&lt;/p&gt;

&lt;p&gt;SQL, on the other hand, is already operating within its wheelhouse when utilized for aforementioned tasks, as was intended when designed by IBM in the early 1970s. While its early versions focused almost solely on data querying and retrieval, over time it evolved and expanded into the robust database management and manipulation system it’s known for today. However, just because databases are its specialty doesn’t mean it’s without its weaknesses: it isn’t particularly adept at working with external databases nor producing the most effective data visualization, for instance — which is where Python can complement SQL beautifully.&lt;/p&gt;

&lt;p&gt;Python, on the other hand, falls rather short when compared to SQL’s ability to manage large datasets and perform complex operations like data retrieval, filtering, indexing, aggregation, etc. Frankly, one could spend days comparing and contrasting the two languages’ strengths and weaknesses which seems like a task both futile and frivolous when the two can simply be paired together to accomplish virtually any and all data-related tasks a programmer (or business) could need. &lt;/p&gt;

&lt;p&gt;In fact, by pairing one of Python’s libraries (like SQLAlchemy or psycopg2, for instance) with SQL’s querying abilities, users are able to write SQL statements within Python code, execute them, and retrieve and process Python objects as results with an ease and grace that I would have dreamt of having at my disposal when previously working in non-profit data evaluation.&lt;/p&gt;

&lt;p&gt;Further, the two languages build data pipelines and perform Extract, Transform, Load (ETL) processes beautifully. While Python can take the helm on extracting data from a multitude of sources, perform complex transformations and prepare such data for loading into a database, SQL queries are able to perform further transformations within the databases themselves, which optimizes performance while reducing the net back-and-forth data transfers can require.&lt;/p&gt;

&lt;p&gt;Finally, integrating SQL’s data retrieval capabilities with Python’s rich visualization libraries allows one to build informative and visually-appealing charts, plots and reports like never before. There are several Python libraries, like Matplotlib, Plotly or Bokeh that serve as supremely effective tools to chart, plot or visualize data, which can then be paired with other libraries that can provide customization options for colors, labels, axis formatting and other visual elements that can provide an exceptionally appealing visual presentation of data. Further, these visualizations can be exported in several different formats — including image or interactive files — which can then be shared with other and/or embedded in web applications or reports. Other Python frameworks like Flask or Django are indispensable when it comes to building interactive web applications to drive home the importance of particular data statistics.&lt;/p&gt;

&lt;p&gt;Ultimately, by pairing Python’s strengths in analytics, external data integrations and data visualization capabilities with SQL’s exceptionally efficient database operations, integrity and scalability both languages are able to overcome their own inherent weaknesses to create impactful, and insightful representations of data that all stakeholders are bound to appreciate when making data-driven decisions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>useEffect(): Making Side Effects More Manageable in React Apps Since 2019</title>
      <dc:creator>Lauren Slayman</dc:creator>
      <pubDate>Sat, 29 Apr 2023 02:26:46 +0000</pubDate>
      <link>https://dev.to/lslayman/useeffect-making-side-effects-more-manageable-in-react-apps-since-2019-eem</link>
      <guid>https://dev.to/lslayman/useeffect-making-side-effects-more-manageable-in-react-apps-since-2019-eem</guid>
      <description>&lt;p&gt;Imagine this: you're building a React app for the first time -- you're drafting up a wireframe and trying to envision how all of the pages and components will fit together and you realize some of them will need to pull a little more weight than others. While some only need to render their particular components, others may require a little more functionality, maybe they're fetching data, updating the DOM or managing a timer; basically, if the component is doing anything other than simply returning a bit of JSX, it's performing &lt;em&gt;side effects&lt;/em&gt;. Luckily, React has special hook for these tasks: &lt;code&gt;useEffect()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For demonstration purposes, let's build a component that counts clicks.&lt;/p&gt;

&lt;p&gt;To start, you'll call &lt;code&gt;useEffect()&lt;/code&gt; in almost the same manner as calling state at the top the of the component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From there, we'll build out the rest of the component, directing it exactly what we want it to produce:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ExampleComponent() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    // This function will be called after every render
    document.title = `You clicked ${count} times`;
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;You clicked {count} times.&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Click me&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, our component is displaying a count as well as a button. When clicked, the button's state is incremented via 'count' and the &lt;code&gt;useEffect()&lt;/code&gt; hook is updating the document title with the current count each time it renders.&lt;/p&gt;

&lt;p&gt;As you can see, when calling &lt;code&gt;useEffect()&lt;/code&gt;, it takes a function as its primary argument (which is being called after each render), and updates the 'document.title' property accordingly.&lt;/p&gt;

&lt;p&gt;Notice that there are no additional arguments to the &lt;code&gt;useEffect()&lt;/code&gt; hook, so it will be called after &lt;em&gt;every&lt;/em&gt; render. If we want it to be called only when the 'count' variable changes, we could simply pass it through as the second argument, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
   document.title = `You clicked ${count} times!`;
}, [count]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is known as a &lt;code&gt;dependency&lt;/code&gt;, which prevents the code from running an endless loop of arguments. And while we have the option to pass the dependencies through arrays like we did above, we can also pass empty arrays through our functions as second arguments, as demonstrated below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function ExampleComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    // This function will be called once on component mount
    fetch('https://example.com/data')
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data))
  }, []);

  return (
    &amp;lt;ul&amp;gt;
      {data.map(item =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.title}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By utilizing the empty array as the second argument, we're telling our component that we only want the side effect to run the &lt;em&gt;first time&lt;/em&gt; our component renders, once again preventing an endless loop.&lt;/p&gt;

&lt;p&gt;Not sure when or how to pass dependencies through the second argument of &lt;code&gt;useEffect&lt;/code&gt;? Here's a short guide!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;useEffect(() =&amp;gt; {})&lt;/em&gt;: No dependencies! This means the side effect will run &lt;strong&gt;every time&lt;/strong&gt; the component renders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; {}, [])&lt;/code&gt;: Once again, there's that empty array! This tells our component that we only want the side effect to run &lt;strong&gt;the first time&lt;/strong&gt; it renders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; {}, [variable1, variable2])&lt;/code&gt;: This one may look new -- it's a dependencies array with elements and it communicates that the side effect should run &lt;strong&gt;whenever the variable(s) change&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although this is just a brief overview, the &lt;code&gt;useEffect()&lt;/code&gt; hook is an invaluable took when it comes to making components more dynamic and interactive, as it allows them to respond to changes in state, user interactions, or even external events. By using this hook to manage side effects, your components' functionality can be optimized to update in real time, interact with external data sources, manage animations and transitions and so much more! I hope you'll practice utilizing this method in your React apps to experience just how powerful individual components can become. Happy coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>"Say what, now?" Breaking Down JavaScript Building Blocks for Algorithmically-Challenged Thinkers</title>
      <dc:creator>Lauren Slayman</dc:creator>
      <pubDate>Fri, 07 Apr 2023 19:26:21 +0000</pubDate>
      <link>https://dev.to/lslayman/say-what-now-breaking-down-javascript-building-blocks-for-algorithmically-challenged-thinkers-4p0p</link>
      <guid>https://dev.to/lslayman/say-what-now-breaking-down-javascript-building-blocks-for-algorithmically-challenged-thinkers-4p0p</guid>
      <description>&lt;p&gt;Reader, if you're anything like me, you probably excel at logical reasoning, but really struggle to translate those concepts into procedural steps. If you're trying to learn to code, but are experiencing challenges turning conceptual &lt;em&gt;why&lt;/em&gt;'s into their procedural &lt;em&gt;how&lt;/em&gt;'s, I'm hoping you'll find this breakdown of JavaScript's three main building blocks: &lt;code&gt;variables&lt;/code&gt;, &lt;code&gt;functions&lt;/code&gt;, and &lt;code&gt;scope&lt;/code&gt; to be of use.&lt;/p&gt;




&lt;h3&gt;
  
  
  JS Variables
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;variable&lt;/strong&gt; is simply the named storage location for a piece of data.&lt;/p&gt;

&lt;p&gt;💡 When naming variables, avoid using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words"&gt;reserved words&lt;/a&gt; or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#future_reserved_words"&gt;future reserved words&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once named, variables then need to be &lt;em&gt;initialized&lt;/em&gt;, which requires two steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declaring (using &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, or &lt;code&gt;var&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Assigning (this is the data to the right of the =)
a. These two steps can be completed in a single line of code, but there will be times when it makes more sense to declare the variable without immediately assigning it a value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When to use &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, or &lt;code&gt;var&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; can lead to scope-related bugs, so it is &lt;strong&gt;&lt;em&gt;almost&lt;/em&gt; never used&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; is the go-to option as its &lt;strong&gt;variables can neither be redeclared nor reassigned&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; values must be assigned immediately when using &lt;code&gt;const&lt;/code&gt; to declare variables!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; should be used &lt;strong&gt;when you know the variable’s value will change&lt;/strong&gt; (i.e. a counter)&lt;/p&gt;

&lt;p&gt;💡 Best practice: always start by declaring variables with &lt;code&gt;const&lt;/code&gt;! If you later realize that its value will need to change over time, go back and adjust it to &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions"&gt;Functions in JS&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A function is essentially a procedure&lt;/strong&gt; — you’re telling JS to perform a task by taking an input and returning an output.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;function declaration&lt;/strong&gt; (aka function definition or statement) is made up of the &lt;code&gt;function&lt;/code&gt; keyword followed by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function &lt;em&gt;name&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The function &lt;em&gt;parameters&lt;/em&gt; (enclosed in parentheses &amp;amp; separated by commas)

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Parameters&lt;/em&gt; are locally-scoped variables that are usable ("scoped") to inside the function. They’re defined inside the function’s declarations &lt;code&gt;()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The JS statements which &lt;em&gt;define&lt;/em&gt; the function (enclosed in curly brackets)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;whatYouWantFunctionToOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Declaring a function&lt;/strong&gt; = giving it a name &amp;amp; telling it what to do, it &lt;strong&gt;does not execute it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A function has to be &lt;em&gt;called&lt;/em&gt; to perform&lt;/strong&gt; the actions with the indicated parameter, like such:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;When called, functions must be in scope; however, function declarations can also be hoisted (appearing below the call), like such:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;whatYouWantFunctionToOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function Return Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Return values are simply values that a function returns when completing its task. &lt;/p&gt;

&lt;p&gt;The values being returned could be one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;expression (such as &lt;code&gt;1 + 1&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a &lt;code&gt;return&lt;/code&gt; is reached in the code, the &lt;code&gt;return&lt;/code&gt; will be executed, but if there is any code after that point, that code will &lt;strong&gt;not&lt;/strong&gt; be executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.w3schools.com/js/js_arrow_function.asp"&gt;Arrow Functions&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Arrow functions allow users to write shorter function syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, this is standard syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whereas this is the same function using the arrow format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Further, if the function has only one statement and the statement returns a value, the brackets &lt;em&gt;and&lt;/em&gt; the &lt;code&gt;return&lt;/code&gt; keyword can be removed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the function has parameters, they’ll be passed inside the parentheses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there’s only one parameter, the parentheses can be removed entirely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Callback Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;callback&lt;/strong&gt; is a &lt;strong&gt;function passed as an argument to another function&lt;/strong&gt;, to be “called back” at a later time (as opposed to being executed immediately).&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 jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//callback function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am a callback function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//passing function as an argument&lt;/span&gt;
&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lauren&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//returned:&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi Lauren I am a callback function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;a href="https://medium.com/@aastha6348/understanding-scopes-and-scope-chain-in-javascript-4d87f7a4b144"&gt;Scope&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In JS, &lt;strong&gt;&lt;em&gt;scope&lt;/em&gt; refers to the current context of the code&lt;/strong&gt;; wherein, which variables and functions can be accessed. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; where you define a variable or function in JS determines where it can be accessed later on!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For instance, if a variable is defined within a function, it cannot be accessed outside of that function.&lt;/p&gt;

&lt;p&gt;There are several types of scope in JS:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mWCX9-sT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2fe4pnke77153l06tis.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mWCX9-sT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2fe4pnke77153l06tis.png" alt="Scope Image" width="650" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global scope&lt;/strong&gt; means that a &lt;strong&gt;variable or function is available&lt;/strong&gt; (and therefore accessible) &lt;strong&gt;anywhere in the code or program.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Global Scoped Variable&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;global_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Flatiron School&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;//First function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;first_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;global_variable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//Second function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;second_func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;first_func&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;second_func&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;//output = Flatiron School&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Local Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local scope&lt;/strong&gt; means that a &lt;strong&gt;variable or function is only available within its code block&lt;/strong&gt;. Local scope is created with curly brackets &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope Chain&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope chain&lt;/strong&gt; is the &lt;strong&gt;process by which JS determines which variables &amp;amp; objects are referenceable&lt;/strong&gt; based off their location in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lexical Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lexical scope&lt;/strong&gt; means that a &lt;strong&gt;variable’s scope is determined by its position in the code&lt;/strong&gt; (namely, they must be located within the same scope as their parent variables).&lt;/p&gt;




&lt;p&gt;As someone with a liberal arts background who has recently elected to step into a new realm of education and career-building, I've struggled greatly with internally translating coding vocabulary into layman's terms to better understand JavaScript's language and building principles. If, reader, you've found yourself with the same struggles, I sincerely hope this breakdown has been of positive use.&lt;/p&gt;

&lt;p&gt;A quick note about sources: the primary sources utilized for this post came from lectures and curriculum provided by Flatiron School. Any external sources have been linked throughout.&lt;/p&gt;

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