Foundations

Data Types and Conditionals

Foundations Course

Introduction

There are a few extremely common types of data that you will encounter in JavaScript, and these lessons on the fundamentals will give you a really strong foundation in all of them. However, before we start digging deep, read through this overview of the most common data types in JavaScript.

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • Name the eight data types in JavaScript.
  • Understand the difference between single, double, and backtick quotes.
  • Embed a variable/expression in a string.
  • Understand what a method is.
  • Name the three logical operators.
  • Understand what the comparison operators are.
  • Understand what conditionals are.
  • Understand what nesting is.
  • Understand what truthy and falsy values are.

Strings

Depending on what kind of work you’re doing, you might end up working more with pieces of text rather than numbers. A string is a piece of text… and is a fundamental building block of the language.

  1. Read and code along with this MDN tutorial on strings in JavaScript. Skip the Concatenation in context section, as it covers concepts we will cover in a later lesson on DOM Manipulation.
  2. Go through the W3Schools lesson on string methods to learn a bit more about what you can do with strings. A method is a bit of functionality built into the language or specific data types.
  3. There are many more built-in string methods than what the previous resource showed. You don’t need to read through this let alone memorize things, but you ought to be aware of the MDN documentation for strings. You’ll find much more information about strings here as well as an exhaustive reference of built-in methods, so bookmark it!

Conditionals

Now it’s time for the fun stuff… So far, we haven’t done much with our programming that you couldn’t do with basic math skills. Sure, we’ve told our computer how to do the math, which makes it quicker, but the essence of programming is teaching the computer how to make decisions to do more involved things. Conditionals are how we do that. Read through the following resources:

  1. Step one in learning about conditionals is making sure you have a good grasp on comparisons.
  2. The MDN article on conditionals is a great introduction to various ways to write conditional code.
  3. JavaScript.info’s lesson on if/else covers the same basic concept (read through it as a review!) and - more importantly - offers the usual tasks at the bottom of the page!
  4. Go through JavaScript.info’s lesson on logical operators. A little heads up regarding this reading’s tasks: there will be questions where you see alert() with a number or string inside the parenthesis. What’s happening here will be discussed later in the curriculum. Some of the answers may not make sense now, but they are accurate, and you will understand them as you progress in the curriculum. Don’t worry too much about it now!
  5. Learn about the switch statement, which is handy when you have multiple conditions.

Assignment

To give you a good bit of practice, we have created JavaScript exercises for you to play with. They contain tests that are used to make sure that your code works like it is supposed to. Anywhere you see return, it just means that when the function finishes running, it will spit back whatever comes after return. In future lessons, we will go into much further detail regarding these concepts, so just hang in there for now!

Be sure to follow the order presented here. Read all the directions, watch the terminal, and read all the errors.

  1. Follow the instructions in the README of our javascript-exercises repository to set up your local environment. Once you have forked the repository, cloned it and installed Jest, review each README file prior to completing the following exercises in order (which are all in the foundations/ directory):
    • 01_helloWorld (This exercise is intentionally very beginner-friendly to ensure that you have everything set up properly!)
    • 02_addNumbers
    • 03_numberChecker
    • 04_mathEquations
    • 05_joinStrings

Note: Solutions for these exercises can be found in the solution folder of each exercise.

Knowledge check

The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

Support us!

The Odin Project is funded by the community. Join us in empowering learners around the globe by supporting The Odin Project!