Syntax Differences: JavaScript vs. Java
Tags: javascript, java, syntax, differences
Published: 2021-02-07 04:51:57

_In this post I'll explain some syntactical differences between JavaScript and Java._

# Declaring and assigning a variable

```
// JAVASCRIPT
let watermelon = "good";


// JAVA
String watermelon = "good";
```

In JavaScript, you can declare a variable with `let`, `const` or `var`. You do not need to specify the variable's data type, like you do in Java. In the Java example above, we need to say the `watermelon` variable will point to the `String`, `"good"`.  

# Printing/logging/outputting something

```
// JAVASCRIPT
console.log("tomatoes are gross");


// JAVA
System.out.println("tomatoes are gross");
```

There is just a minor difference between printing in JavaScript vs. Java. Printing the contents of an array works a bit differently in Java compared to JavaScript, which I'll cover later in this post. 

# Variable interpolation

```
// JAVASCRIPT
let fruit = "blueberry";
console.log(`My favorite fruit is the ${fruit}`);


// JAVA
String fruit = "blueberry";
System.out.println(
  String.format("My favorite fruit is the %s", fruit)
);
```

To put a variable in a string: 
* _In JavaScript_, inside the `console.log`, write your string with back ticks; insert `${variableName}` where you want your variable to go. 

* _In Java_, inside the `System.out.println`, use `String.format()` to write your string; insert `%s` where you want your variable to go; after the ending quotation mark of the string, place a comma then write your variable name. 

# Declaring, invoking, and printing the return value of a function/method

```
// JAVASCRIPT
const isEven = (num) => {

  // do something with num and return the result
  return (num % 2 === 0) ? true : false;
}

console.log(isEven(4));




// JAVA
public class Main {
  static boolean isEven(int num) {

    // do something with num and return the result
    return (num % 2 == 0) ? true : false;
  }

  public static void main(String[] args) {
    System.out.println(isEven(4));
  }
}
```

In both languages, we're passing an integer of `4` to the `isEven` function/method. Then, we determine whether the remainder of the integer divided by 2 is 0. If it is, it's an even number so we return `true`; else, the integer is not even so we return `false`. 

In JavaScript, you can declare and invoke a function with the following general syntax: 

```javascript
// declare a function called functionName
const functionName = (parameter1, parameter2, ...) => {
  // return something
}

// invoke the function called functionName
functionName(argument1, argument2, ...);
```

However, in Java, every method needs to be declared within a class (in the above example, the class is `Main`). Every parameter to a method needs to have a data type. If the method returns something, you need to specify the return value's data type (`void` means nothing is returned). Every method must be invoked within the `main` method (The Java compiler will look for a `main` method before it executes any other code). 

The following is the general syntax for declaring and invoking a method in Java: 

```java
public class Main {

  // declare a function called functionName
  static returnValueDataType functionName(parameterOneDataType 
  parameterOne, parameterTwoDataType parameterTwo, ...) {

    // return something 

  }


  public static void main(String[] args) {

    // invoke the function called functionName
    functionName(argument1, argument2, ...);
  }
}
```

# Declaring and printing an array

```
// JAVASCRIPT: 
let array = ["strawberry", "orange", "lemon"];
console.log(array);


// JAVA:
public class Main {
  public static void main(String[] args) {
    String[] array = {"strawberry", "orange", "lemon"};
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }
}
```

In JavaScript, the elements in an array go inside of square [] brackets. In Java, the elements in an array go inside curly {} braces. Also in Java, you need to specify what data type the elements in the array will be. This is why we write `String[]` above. The `String` tells Java the array elements will be `String`s. The `[]` tells Java you're creating an array. 

Since Java doesn't natively print out the contents of an array with `System.out.println(arrayName)`, you need to loop through the array, printing each element for each iteration. This is why the above example uses a `for` loop to print each element in the array.