Difference between JavaScript and Java

  1. Use var when declaring variables.
  2. The number of primitive data types is floating point and the result of division is floating point.
  3. You can use a string in () of switch(). (Since JDK 1.7, that is available in Java)
  4. Creating an ordinary array that can access elements using indexes
  5. An associative array is an array of key: value. Associative arrays can not access an element with an index. Java has no associative array.
  6. for in statement
  7. JavaScript can pass a function as an argument to a function. (Since JDK 1.8, Java can also pass a lambda expression as an argument)

1,2 have already confirmed.

3. switch("string")

var str = "A";

switch (str) {
case "A":
	alert("A");
	break;
case "B":
	alert("B");
	break;
case "C":
	alert("C");
	break;
case "D":
	alert("D");
	break;
default:
	alert("F");
}

4. Create ordinary array

Using []

An array created using [] can access an element using an index.

var arr = [1, 2, 3, 4, 5];
var sum = 0;
for (var i = 0; i < arr.length; i++) {
	sum = sum + arr[i];
}
alert('SUM:' + sum);

Using Array object

An array created using an Array object can also be accessed using an index.

var arr = new Array(1, 2, 3, 4, 5);
var sum = 0;
for (var i = 0; i < arr.length; i++) {
	sum = sum + arr[i];
}
alert('SUM:' + sum);

5. Associative array

Associative arrays are created using {}.

var person = {"name":"El Draque", "job":"pirate"};
alert(person.name);
alert(person["name"]);
alert(person.job);
alert(person["job"]);

6. You can access all the elements of the associative array with the for in loop.

var person = {"name":"Barbadosed", "job":"pirate"};
var result = "";
for(var property in person) {
	result += person[property];
}
alert(result);

7. JavaScript can pass a function as an argument to a function.

function x(a, y) {
    var ret = y(a);
    alert(ret);
}

function z(a) {
    return a * a
}

x(2, z);