HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Javascript Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: Hello World!
  3. Lesson 3: Variables
  4. Lesson 4.1: Arithmetic and Operators - Numeric Arithmetic
  5. Lesson 4.2: Arithmetic and Operators - Boolean and String Arithmetic

Javascript Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 4.2: Arithmetic and Operators – Boolean and String Arithmetic

del.icio.us Digg Blinklist reddit

In our previous lesson, we went over some of the basic operators of Javascript and discussed numeric arithmetic. In this lesson we are going to talk about boolean and string “arithmetic”.

What are Booleans?

Booleans are variable types that only have two possible values: true and false. This are most commonly used in relationship to conditional control statements, which are used to control the flow of the code.

Boolean-Related Operators

There are several operators related to booleans that we are going to discuss: == (Boolean EQUALITY), || (Boolean OR), && (Boolean AND), < (Boolean LESSER THAN, > (Boolean GREATER THAN), <= (Boolean LESSER THAN OR EQUAL), >= (Boolean GREATER THAN OR EQUAL), ! (Boolean NOT).

Equality Test

The equality sign is a double-equal sign so it can be distinguished from the assignment operator (=) which we use to assign values to variables. This operator is used to compare any two values and determine if they are equal to one another. If they are, the result is true; if they are not, the result is false.

For example, say we have a numeric value and we want to see if it equals 5, we would do:

var result = (myNum == 5);

This would compare myNum to 5. If they are equal, it would assign result the value of “true”, otherwise it would assign the value of false. We will also use these type of equations in control structures such as if statements, which will look something like this:

if(myNum == 5)
    alert("It is 5!");
else
     alert("It isn't 5.");

We’ll discuss if statements, as well as the other control structures in our next lessons.

Keep in mind that the equality operator can compare anything, not just numbers. It can compare strings, booleans, or any other type of variable or object.

AND/OR

The Boolean AND and OR operators are similar in that they compare two boolean values together and give another boolean value. The difference is in what they return. The Boolean AND operator returns true if both the boolean values on the left and right side are true. The Boolean OR operator will return true if the either the value on the left or right are true.

These can be used to test multiple conditions together. You can link multiple AND/ORs together into one long statement. Each has the same level of precedence in the equation, so they are simply worked left-to-right.

For more on Boolean operators, take a look at the Programming lesson on Boolean Arithmetic. The only difference is that Javascript uses && instead of AND and || instead of OR. Also, there is an XOR mentioned. Javascript doesn’t have an explicit XOR operator, however this is equivalent:

var result = (a || b && !(a && b)); // Same as (a XOR b)

It isn’t a common occurrence to need an XOR, but it is good to know how to for that one time that you do.

String “Arithmetic”

Okay, you got me, technically you can’t do arithmetic with strings. But, you can still add them together (called “concatenation”). This will simply append the first string to the end of the second string, like so:

var string1 = "I am ";
var string2 = "Bob";
var string3 = string1 + string2; // string3 now equals "I am Bob";

We’ll discuss strings more in-depth in a future lesson all about strings.


In this lesson we discussed the various types of arithmetic that you can perform in Javascript. In our next lesson we’ll take a look at control statements, which will let us control the flow of our code based on the values of variables.

This entry was posted on Sunday, November 8th, 2009 at 7:55 pm and is filed under Javascript Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design