A comparison operator compares two values and returns the result "true" or "false". For comparing numeric values the operators perform the same as they do in algebra. For comparing character strings each character in the strings are converted to their ASCII values and then a numeric comparison is made.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Java Script Comparison Operators

A comparison operator compares two values and returns the result "true" or "false". Below is a list of comparison operators.

Operator    Tests For
==two values are equal
!=two values are not equal
>first value is greater than second value
>=first value is equal to or greater than second value
<first value is less than second value
<=first value is equal to or less than second value

For comparing numeric values the operators perform the same as they do in algebra. For comparing character strings, each character in the strings is converted to its ASCII value and then a numeric comparison is made of the ASCII values all the characters of the two character strings.

The ASCII values of upper case characters are not the same as their lower case values. When you want to compare two character strings disregarding case, use the string objects .toUpperCase or .toLowerCase methods to set the characters of the two strings to the same case.

var strFirst = "My Character String";
var strSecond = "my character string";

if(strFirst == strSecond)
  alert("The strings are equal");
else
  alert("The strings are NOT equal");

if(strFirst.toLowerCase() == strSecond.toLowerCase())
  alert("The strings are equal");
else
  alert("The strings are NOT equal");

A common test used in form validation is to compare the contents of a text box with an empty string, as shown below.

if(form.inputbox.value != "")
  alert("Enter input value");

Comparing Different Data Types

If you compare a numeric value to a string value, Java Script automatically converts the string to a number before performing the comparison.

var numValue = 123;
var strValue = "123";

if(numValue == strValue)
  alert("The numbers are equal");
else
  alert("The numbers are NOT equal");

You may want to compare other dissimilar data types such as a date to a number, or you may just not want to accept the ambiguity of Java Script's internal data type conversions. In that case you could do an explicit data type conversion before performing the comparison.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269