Compare 2 variables in javascript

I want compare two variables, that are strings, but I am getting an error.


    var to_check=$[this].val[];
    var cur_string=$["#0"].text[];
    var to_chk = "that";
    var cur_str= "that";

    if[to_chk==cur_str]{
        alert["both are equal"];
        $["#0"].attr["class","correct"];    
    } else {
        alert["both are not equal"];
        $["#0"].attr["class","incorrect"];
    }

Is something wrong with my if statement?

asked Apr 1, 2013 at 5:15

10

=== is not necessary. You know both values are strings so you dont need to compare types.

function do_check[]
{
  var str1 = $["#textbox1"].val[];
  var str2 = $["#textbox2"].val[];

  if [str1 == str2]
  {
    $[":text"].removeClass["incorrect"];
    alert["equal"];
  }
  else
  {
    $[":text"].addClass["incorrect"];
    alert["not equal"];
  }
}
.incorrect
{
  background: #ff8888;
}





check

answered Apr 1, 2013 at 9:56

Anders LindénAnders Lindén

6,4647 gold badges51 silver badges104 bronze badges

1

instead of using the == sign, more safer use the === sign when compare, the code that you post is work well

answered Apr 1, 2013 at 9:40

anztraxanztrax

7457 silver badges9 bronze badges

2

I used below function to compare two strings and It is working good.

function CompareUserId [first, second]
{

   var regex = new RegExp['^' + first+ '$', 'i'];
   if [regex.test[second]] 
   {
        return true;
   }
   else 
   {
        return false;
   }
   return false;
}

slfan

8,805115 gold badges65 silver badges77 bronze badges

answered Oct 3, 2017 at 5:39

You can use javascript dedicate string compare method string1.localeCompare[string2]. it will five you -1 if the string not equals, 0 for strings equal and 1 if string1 is sorted after string2.


    var to_check=$[this].val[];
    var cur_string=$["#0"].text[];
    var to_chk = "that";
    var cur_str= "that";
    if[to_chk.localeCompare[cur_str] == 0]{
        alert["both are equal"];
        $["#0"].attr["class","correct"];    
    } else {
        alert["both are not equal"];
        $["#0"].attr["class","incorrect"];
    }

answered Mar 3, 2019 at 18:11

Kiran ChennaKiran Chenna

1,40210 silver badges9 bronze badges

Comparison and Logical operators are used to test for true or false.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x = 5, the table below explains the comparison operators:

How Can it be Used

Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if [age < 18] text = "Too young to buy alcohol";

You will learn more about the use of conditional statements in the next chapter of this tutorial.

Logical Operators

Logical operators are used to determine the logic between variables or values.

Given that x = 6 and y = 3, the table below explains the logical operators:

OperatorDescriptionExampleTry it
&& and [x < 10 && y > 1] is true Try it »
|| or [x == 5 || y == 5] is false Try it »
! not ![x == y] is true Try it »

Conditional [Ternary] Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

Syntax

variablename = [condition] ? value1:value2 

Example

If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".

Comparing Different Types

Comparing data of different types may give unexpected results.

When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false.

CaseValueTry
2 < 12 true Try it »
2 < "12" true Try it »
2 < "John" false Try it »
2 > "John" false Try it »
2 == "John" false Try it »
"2" < "12" false Try it »
"2" > "12" true Try it »
"2" == "12" false Try it »

When comparing two strings, "2" will be greater than "12", because [alphabetically] 1 is less than 2.

To secure a proper result, variables should be converted to the proper type before comparison:

age = Number[age];
if [isNaN[age]] {
  voteable = "Input is not a number";
} else {
  voteable = [age < 18] ? "Too young" : "Old enough";
}

Try it Yourself »

The Nullish Coalescing Operator [??]

The ?? operator returns the first argument if it is null or undefined. Otherwise it returns the second.

Example

let name = null;
let text = "missing";
let result = name ?? text;

Try it Yourself »

The nullish operator is supported in all browsers since March 2020:

Chrome 80 Edge 80 Firefox 72 Safari 13.1 Opera 67
Feb 2020 Feb 2020 Jan 2020 Mar 2020 Mar 2020

The Optional Chaining Operator [??]

The ?. operator returns undefined if an object is undefined or null [instead of throwing an error].

Example

// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Ask for car name:
document.getElementById["demo"].innerHTML = car?.name;

Try it Yourself »

The optional chaining operator is supported in all browsers since March 2020:

Chrome 80 Edge 80 Firefox 72 Safari 13.1 Opera 67
Feb 2020 Feb 2020 Jan 2020 Mar 2020 Mar 2020



What is === and == in JavaScript?

The strict equality operator [ === ] checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

How do you compare two things in JavaScript?

Comparing objects is easy, use === or Object.is[]. This function returns true if they have the same reference and false if they do not. Again, let me stress, it is comparing the references to the objects, not the keys and values of the objects. So, from Example 3, Object.is[obj1,obj2]; would return false.

How do you compare two variables?

Use scatterplots to compare two continuous variables. Use scatterplot matrices to compare several pairs of continuous variables. Use side-by-side box plots to compare one continuous and one categorical variable. Use variability charts to compare one continuous Y variable to one or more categorical X variables.

Which operator is used to compare 2 variables in JavaScript?

== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values.

Chủ Đề