JavaScript Resources: JavaScript vs Java: Quick Reference

Goal

Compare JavaScript syntax and concepts to what you know from Java, focusing on key differences that commonly trip up Java developers.

Quick Reference

Topic Description
Variable Declarations let, const vs Java types
Function Syntax Function declarations and arrow functions
Equality Operators == vs === (use ===!)
Loops Similar to Java, but with let
Arrays Dynamic arrays with mixed types
String Formatting Template literals vs format strings
Practice Exercises Hands-on conversion exercises
Common Pitfalls Things to watch out for

Key Differences Overview

1. Variable Declarations

Java:

int age = 25;
String name = "Alice";
final int MAX_SIZE = 100;  // constant

JavaScript:

let age = 25;              // can be reassigned
const name = "Alice";       // cannot be reassigned (like Java's final)
const MAX_SIZE = 100;       // constant

Key Points:

  • JavaScript uses let and const (not var in modern code)
  • No type declarations - JavaScript is dynamically typed
  • const means the variable cannot be reassigned (but object properties can change)

2. Function Syntax

Java:

public static int add(int a, int b) {
    return a + b;
}

JavaScript (Function Declaration):

function add(a, b) {
    return a + b;
}

JavaScript (Arrow Function):

const add = (a, b) => {
    return a + b;
};

// Or shorter:
const add = (a, b) => a + b;

Key Points:

  • No return type or parameter types
  • Function declarations are “hoisted” (can be called before they’re defined)
  • Arrow functions are NOT hoisted

3. Equality Operators

Java:

if (a == b) { }  // compares values (primitives) or references (objects)

JavaScript:

if (a == b) { }   // loose equality (type coercion)
if (a === b) { }  // strict equality (no type coercion) - USE THIS!

Examples:

5 == "5"   // true (type coercion)
5 === "5"  // false (strict comparison)

null == undefined   // true
null === undefined  // false

Key Point: Always use === unless you have a specific reason to use ==.

4. Loops

Java:

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

JavaScript:

for (let i = 0; i < 10; i++) {
    console.log(i);
}

Key Points:

  • Very similar syntax!
  • Use let instead of type declaration
  • console.log() instead of System.out.println()

5. Arrays

Java:

int[] numbers = {1, 2, 3};
String[] names = new String[5];

JavaScript:

const numbers = [1, 2, 3];
const names = [];  // empty array

Key Points:

  • Arrays are objects in JavaScript (not a separate type)
  • Arrays can hold mixed types: [1, "hello", true]
  • Arrays are dynamic (no fixed size)

6. String Formatting

Java:

String name = "Alice";
int age = 25;
String message = String.format("Hi, I'm %s and I'm %d years old.", name, age);
// Or using printf-style:
System.out.printf("Hi, I'm %s and I'm %d years old.%n", name, age);

JavaScript (Template Literals):

const name = "Alice";
const age = 25;
const message = `Hi, I'm ${name} and I'm ${age} years old.`;

Key Points:

  • JavaScript uses template literals (backticks `) instead of format strings
  • Variables are embedded using ${variableName}
  • Template literals can span multiple lines
  • Expressions can be used inside ${}: `Result: ${a + b}`

Practice Exercises

Exercise 1: Convert Java to JavaScript

Convert the following Java code to JavaScript:

Java:

public class Calculator {
    public static int multiply(int a, int b) {
        return a * b;
    }
    
    public static void main(String[] args) {
        int result = multiply(5, 3);
        System.out.println("Result: " + result);
    }
}

JavaScript:

function multiply(a, b) {
    return a * b;
}

const result = multiply(5, 3);
console.log("Result: " + result);

Or using an arrow function:

const multiply = (a, b) => a * b;
const result = multiply(5, 3);
console.log("Result: " + result);

Exercise 2: Type Coercion

What will each of these print? (Try to figure it out before running the code!)

console.log(5 == "5");
console.log(5 === "5");
console.log(0 == false);
console.log(0 === false);
console.log(null == undefined);
console.log(null === undefined);

console.log(5 == "5");        // true (type coercion)
console.log(5 === "5");      // false (strict equality)
console.log(0 == false);     // true (type coercion)
console.log(0 === false);    // false (strict equality)
console.log(null == undefined);  // true (special case)
console.log(null === undefined); // false (strict equality)

Exercise 3: Function Hoisting

What will this code print?

console.log(add(2, 3));

function add(a, b) {
    return a + b;
}

What about this?

console.log(subtract(5, 2));

const subtract = (a, b) => a - b;

First example prints 5 - function declarations are hoisted, so they can be called before they’re defined.

Second example throws an error - arrow functions are NOT hoisted, so you can’t call them before they’re defined.

Common Pitfalls for Java Developers

  1. Forgetting ===: Always use strict equality (===) unless you specifically need type coercion
  2. Type assumptions: JavaScript variables can change types - don’t assume a variable stays the same type
  3. Array vs Object: Arrays are objects in JavaScript, which can be confusing
  4. No access modifiers: JavaScript doesn’t have public, private, protected - everything is accessible
Loading quiz...

UNC Asheville Department of Computer Science