Code Monkey home page Code Monkey logo

phase-0-pac-1-booleans's Introduction

Boolean Expressions

Learning Goals

  • Explain What a Boolean Expression Is
  • Describe Truthy and Falsey Values in JavaScript

Introduction

Quite commonly in our computer programs, we want to execute code conditionally, i.e., if some condition is true (or false). For example:

  • If the user has entered the correct login credentials, then log them into our web app
  • If there isn't enough money in the user's account to execute the transfer, then send them an error message

In the following lessons, we will learn about operators we can use to construct those conditional statements. Before we get to that, however, we need to understand boolean expressions and the concepts of truthy-ness and falsey-ness.

Explain What a Boolean Expression Is

We have learned that expressions are code statements that return a value: the constant expression returns the value of the constant, the assignment expression returns the value that is assigned, and the variable lookup expression returns the value of a variable. We have also learned that JavaScript has a Boolean data type that has only two values: true or false. As you might expect, therefore, a Boolean expression is an expression that returns either true or false.

Describe the Difference Between Truthy and Falsey Values

All values in Javascript, of any data type (String, Number, etc.), are treated as either truthy or falsey. The following values are falsey:

  • false
  • null
  • undefined
  • 0
  • NaN
  • An empty string ("", '')

Every other value is truthy.

To check whether a value is truthy or falsey, we can pass it to the global Boolean object, which converts the value into its boolean equivalent. You may want to try these out yourself in replit.

Boolean(false);
// => false

Boolean(null);
// => false

Boolean(undefined);
// => false

Boolean(0);
// => false

Boolean(NaN);
// => false

Boolean("");
// => false

Boolean(true);
// => true

Boolean(42);
// => true

Boolean("Hello, world!");
// => true

Don't worry if this seems a little theoretical at this point. We will learn how this information is used in constructing Boolean expressions in the following lessons.

Conclusion

In this lesson, we've learned what a Boolean expression is. We've also learned which values in JavaScript are considered truthy and which are considered falsey. With these understandings, we are now ready to learn how to construct Boolean expressions that return true or false.

Resources

phase-0-pac-1-booleans's People

Contributors

ihollander avatar jlboba avatar lizbur10 avatar maxwellbenton avatar sgharms avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-0-pac-1-booleans's Issues

Boolean not defined in REPL

Canvas Link

https://learning.flatironschool.com/courses/6601/pages/boolean-expressions?module_item_id=510645

Concern

There is a suggestion to try out Booleans in REPL. But when I go to the REPL I have open, run to clear, it produces this:

Hint: hit control+c anytime to enter REPL.
 boolean(true)
ReferenceError: boolean is not defined
 boolean(false)
ReferenceError: boolean is not defined
 boolean(0)
ReferenceError: boolean is not defined

Maybe I've done something wrong. But seems like an issue others would run into?

Thanks! Really enjoying the pre-work so far. Well explained, good baby steps chunks, excited for the course. :)

Additional Context

No response

Suggested Changes

No response

Truthy and Falsy

In the lesson, Falsey is not the correct spelling , it should be falsy ( at least that is my impression)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.