Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Latest commit

 

History

History
600 lines (560 loc) · 9.38 KB

javascript-for-java-dev.md

File metadata and controls

600 lines (560 loc) · 9.38 KB

Javascript for Java dev

Comments

Java Javascript
// single line comments
/* multi
   line comments */
/**
 * Docstring
 * @params {String} a - blablabla
 */
// single line comments
/* multi
   line comments */
/**
 * Docstring
 * @params {string} a - blablabla
 */

Semicolon

Java Javascript
someStatement(); // with semicolon
someStatement() // without semicolon, error
someStatement(); // with semicolon
someStatement() // without semicolon, ok

Arithmetic Operator

Java Javascript
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1 // = 7
10 * 2; // = 20
35 / 5; // = 7
5 / 2; // = 2
5f / 2f; // = 2.5f
5.0 / 2.0; // = 2.5
10 % 2; // = 0
30 % 4; // = 2
1 + 1; // = 2
0.1 + 0.2; // = 0.30000000000000004
8 - 1 // = 7
10 * 2; // = 20
35 / 5; // = 7
5 / 2; // = 2.5
5.0 / 2.0; // = 2.5
10 % 2; // = 0
30 % 4; // = 2

Semua angka di Javascript disimpan sebagai 64-bit IEEE 754 double, sedangkan Java memiliki int, float, dan double.

Precedence

Java Javascript
(1 + 3) * 2; // = 8
2 + (2 - 1) * 8 // = 10
(1 + 3) * 2; // = 8
2 + (2 - 1) * 8 // = 10

Infinity

Java Javascript
Double.POSITIVE_INFINITY
Double.NEGATIVE_INFINITY
Infinity
-Infinity

Boolean

Java Javascript
true
false
true
false

String & Char

Java Javascript
"Something" // String
'c' // char
"Something" // string
'Something' // string
"s" // string
's' // string

Negation

Java Javascript
!true // = false
!false // = true
!true // = false
!false // = true

Equality

Java Javascript
1 == 1 // = true
1 != 1 // = true
'1' == 1 // = error
"false" == "bar" // = true
"false" == false // = error
"false".equals("bar") // = false
// equality check
1 == 1 // = true
1 != 1 // = true
'1' == 1 // = true
"false" == "bar" // = false
"false" == false // = true
// strict equality check
1 === 1 // = true
1 !== 1 // = true
'1' === 1 // = false
"false" === "bar" // = false
"false" === false // = false

Comparison

Java Javascript
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true
1 < 10; // = true
1 > 10; // = false
2 <= 2; // = true
2 >= 2; // = true

String Concatenation (and more)

Java Javascript
"Hello " + "world!"; // = "Hello world!"
"1, 2, " + 3; // = "1, 2, 3"
"Hello " + "world!"; // = "Hello world!"
"1, 2, " + 3; // = "1, 2, 3"
"Hello " + ["world", "!"]; // = "Hello world,!"
"a" < "b"; // = true
"This is a string".charAt(0); // = 'T'
"Hello".length(); // = 5
"This is a string".charAt(0); // = 'T'
"Hello".length; // = 5

Truthy / Falsy

Java Javascript
null
null;
undefined;
// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.
// Note that 0 is falsy and "0" is truthy, even though 0 == "0".

Variables

Java Javascript
String name = "Manusia bernapas";
int age = 12;
boolean hasHighscore = true;
int a = 0, b = 0, c = 0;
// 3 keywords
name = "Manusia Bernapas";
var name = "Manusia Bernapas";
let name = "Manusia Bernapas";
const name = "Manusia Bernapas";
const a = 0, b = 0, c = 0;

Shorthand

Java Javascript
i++;
i--;
++i;
--i;
i += 1;
i -= 2;
i *= 3;
i++;
i--;
++i;
--i;
i += 1;
i -= 2;
i *= 3;

Array / List

Java Javascript
String[] foo = { "1", "2", "3" };
foo[2] // = "3"
foo.size() // = 3
foo[2] = "4" // = { "1", "2", "4" }
const foo = ["1", 30, false];
foo[2] // = 30
foo.push("World");
foo.length; // = 4
foo[2] = 123 // = ["1", 30, 123]

Object

Java Javascript
// sebenernya bisa pake class
// tapi nanti di Javascript juga ada class yang lebih mirip
// class di Javascript under the hood itu pakenya object juga
// tapi dikasih syntactical sugar biar mirip kaya class biasanya
const person = {
  name: "Manusia Bernapas",
  age: 12,
  "has highscore": true
}
person.name // = "Manusia Bernapas"
person.age = 20 // person.age === 20
person["has highscore"] // = true

Control Structure

Java Javascript
int number = 2;
if (number > 2) {
  System.out.println("Number is bigger than 2");
} else if (number == 2) {
  System.out.println("Number is equal to 2");
} else {
  System.out.println("Number is smaller or equal to 2");
}
const number = 2;
if (number > 2) {
  console.log("Number is bigger than 2");
} else if (number === 2) {
  console.log("Number is equal to 2");
} else {
  console.log("Number is smaller or equal to 2");
}
while (condition) {
  // do something
}
while (condition) {
  // do something
}
do {
  // do something
} while (condition)
do {
  // do something
} while (condition)
for (int i = 0; i < 10; i++) {
  System.out.println(i);
}
for (let i = 0; i < 10; i++) {
  console.log(i);
}
char grade = 'B';
switch (grade) {
  case 'A':
    System.out.println("Great job");
    break;
  case 'B':
    System.out.println("OK job");
    break;
  case 'C':
    System.out.println("You can do better");
    break;
  default:
    System.out.println("Oy vey");
    break;
}
const grade = 'B';
switch (grade) {
  case 'A':
    console.log("Great job");
    break;
  case 'B':
    console.log("OK job");
    break;
  case 'C':
    console.log("You can do better");
    break;
  default:
    console.log("Oy vey");
    break;
}

Function / Method

Java Javascript
String upper(String text) {
  return text.toUpperCase();
}
upper("foo"); // = "FOO"
function upper(text) {
return text.toUpperCase();
}
upper("foo"); // = "FOO"
(String text) -> text.toUpperCase();
const upper = (text) => {
  return text.toUpperCase()
};
const upper = (text) => text.toUpperCase();
upper("foo"); // = "FOO"
[1, 2, 3].map((f) => f * 2) // = [2, 4, 6]
[1, 2, 3].map((f) => f * 2) // = [2, 4, 6]