javaScriptIsTheSameAsJava = false; Part 2: Comparisons and Conditionals

Steven Kandow
The Startup
Published in
4 min readOct 14, 2020
Image taken from design by Camilo Garcia — Pixabay

Last week, we began our basic comparison between Java and JavaScript, taking a look at the basic structure of completing a simple printing task, how to declare and assign a variable, and how to manipulate a variable via basic mathematic operations.

In this entry, we’ll take a look at how the two languages compare values and utilize conditional values.

Value Comparisons

For the following operations, let’s define a few variables:

In JavaScript:

mathComparisons.js
--------------------
const valueOne = 12;
const valueTwo = 7.5;
const valueThree = 3;
const valueFour = 12;
const valueFive = “I’m a string”;
const valueSix = “I”;

And Java:

MathComparisons.java
--------------------
public class MathComparisons {
public static void main(String[] args) {
int valueOne = 12;
double valueTwo = 7.5;
int valueThree = 3;
int valueFour = 12;
String valueFive = “I’m a string”;
char valueSix = “I”;
}
}

With these values in mind, let’s look at some comparisons in the two languages:

JavaScript:

mathComparisons.js
--------------------
console.log(valueOne > valueTwo);
console.log(valueTwo < valueThree);
console.log(valueOne >= valueThree);
console.log(valueTwo <= valueFour);
//=> true
//=> false
//=> true
//=> true

Java:

MathComparisons.java
--------------------
System.out.println(valueOne > valueTwo);
System.out.println(valueTwo < valueThree);
System.out.println(valueOne >= valueThree);
System.out.println(valueTwo <= valueFour);
//=> true
//=> false
//=> true
//=> true

For both JavaScript and Java, the symbols for greater-than (>), less-than (<), greater-than-or-equal-two (>=), and less-than-or-equal-to (<=) are the same. These expressions return a Boolean value.

Let’s look at comparing equivalency.

JavaScript:

mathComparisons.js
--------------------
console.log(valueOne === valueTwo);
console.log(valueOne === valueFour);
console.log(valueTwo !== valueThree);
console.log(valueFour !== valueOne);
//=> false
//=> true
//=> true
//=> false

Java:

MathComparisons.java
--------------------
System.out.println(valueOne == valueTwo);
System.out.println(valueOne == valueFour);
System.out.println(valueTwo != valueThree);
System.out.println(valueFour != valueOne);
//=> false
//=> true
//=> true
//=> false

There is just one syntactical difference here: for strict comparison in JavaScript utilizes three equals signs (===). Java utilizes two for that same comparison (==). Similarly, the not-equals sign in Java has one less equals-sign that its corresponding symbol in JavaScript.

JavaScript can also utilize two equals signs for comparison, but this comparison is less strict and is prone to errors. See my blog on this topic for more information.

Lastly, let’s compare our non-number values:

JavaScript:

mathComparisons.js
-------------------
console.log(valueFive === valueSix);
console.log(valueFive !== valueSix);
//=> false
//=> true

Java:

MathComparisons.java
--------------------
System.out.println(valueFive.equals(valueSix));//=> false

Regarding strings and characters, JavaScript can compare sameness/equality in the same way as other data types. Java cannot do this; the equals() method is required. Java also does not have anything equivalent to a notEquals() method in its basic library.

Conditional Statements

Let’s keep our defined variables the same as we look at a conditional statement written out in JavaScript and Java.

JavaScript:

mathComparisons.js
--------------------
if (valueOne === valueTwo) {
console.log(“Activated the if statement”);
} else if (valueOne === valueFour) {
console.log(“Activated the else if statement”);
} else {
console.log(“Activated the else statement”);
};
//=> Activated the else if statement

Java:

MathComparisons.java
--------------------
if (valueOne == valueTwo) {
System.out.println(“Activated the if statement”);
} else if (valueOne == valueFour) {
System.out.println(“Activated the else if statement”);
} else {
System.out.println(“Activated the else statement”);
};
//=> Activated the else if statement

Notice that the syntax of if, else if, and else are identical between the two languages.

Both JavaScript and Java can utilize what’s known as the ternary operator, which consists of three parts, the conditional, followed by a ?, what to execute if the statement is true, followed by a :, and lastly what to execute if the statement is false. However, in Java, this operator is typically limited to assigning a value to a variable.

JavaScript:

mathComparisons.js
--------------------
valueOne === valueFour ? console.log(“This is true”) : console.log(“This is false”);//=> This is true

Java:

MathComparisons.javaString verdict = valueOne == valueFour ? “This is true” : “This is false”System.out.println(verdict)//=> This is true

Both languages also allow for the use of switch-case statements:

JavaScript:

mathComparisons.js
--------------------
switch (valueSix) {
case ‘A’:
console.log(“I’d like to buy an A”);
break;
case ‘E’:
console.log(“I’d like to buy an E”);
break;
case ‘I’:
console.log(“I’d like to buy an I”);
break;
case ‘O’:
console.log(“I’d like to buy an O”);
break;
case ‘U’:
console.log(“I’d like to buy a U”);
break;
default:
console.log(“I’d like to spin”);
}
//=> I’d like to buy an I

Java:

MathComparisons.java
--------------------
switch (valueSix) {
case ‘A’:
System.out.println(“I’d like to buy an A”);
break;
case ‘E’:
System.out.println(“I’d like to buy an E”);
break;
case ‘I’:
System.out.println(“I’d like to buy an I”);
break;
case ‘O’:
System.out.println(“I’d like to buy an O”);
break;
case ‘U’:
System.out.println(“I’d like to buy a U”);
break;
default:
System.out.println(“I’d like to spin”);
}
//=> I’d like to buy an I

Lastly, let’s look at how JavaScript and Java handle conditionals with multiple conditions:

Both utilize && for “and” and || for “or”. However, only JavaScript requires each condition to be wrapped in its own set of parenthesis.

JavaScript:

mathComparisons.js
--------------------
if ((valueOne > valueTwo) && (valueTwo > valueThree)) {
console.log(“valueTwo is in the middle”)
} else {
console.log(“valueTwo is not in the middle”)
}
if ((valueOne < valueTwo) || (valueOne === valueFour)) {
console.log(“One of these is true”)
} else {
console.log(“Neither of these are true”)
}
//=> valueTwo is in the middle
//=> One of these is true

Java:

MathComparisons.java
--------------------
if (valueOne > valueTwo && valueTwo > valueThree) {
System.out.println(“valueTwo is in the middle”);
} else {
System.out.println(“valueTwo is not in the middle”);
};
if (valueOne < valueTwo || valueOne === valueFour) {
System.out.println(“One of these is true”);
} else {
System.out.println(“Neither of these are true”);
};
//=> valueTwo is in the middle
//=> One of these is true

In the next entry in our series, we’ll take a look at how arrays are built and manipulated in these two languages.

--

--