跪拜 Guibai
← Back to the summary

let Over var, === Over ==: The JavaScript Essentials Kotlin Devs Need

1. Variable Declarations (no type needed, semicolons optional)

In Kotlin, val stands for value, an immutable value; var stands for variable, a mutable variable.

In JS, const stands for constant, an immutable constant; let is just let, as in "let's go", meaning "allow" or "make". let x = 1 means let x be assigned to 1, or set x to 1, make x 1.

Question: Why doesn't JS use var instead of let?

2. Data Structures: Arrays & Objects (JSON)

const list = ["A", "B", "C"]          // array: list[0] gets "A"
const user = { name: "Tom", age: 18 } // object (key-value pairs): user.name gets "Tom"

Objects in JS are written directly as JSON, no need to define a structure in advance. You can create an object out of thin air with { name: "Tom", age: 18 } (called an object literal), and even add new properties anytime: user.gender = "male".

Fun fact: JSON stands for JavaScript Object Notation actually copied JS object syntax when it was born.

Question: What if multiple objects want to reuse the same structure?

There are 3 common approaches:

3. Common Operators

Question: Why does JS use === and !==? Why not just use == and !=, one less character? Does == and != have other uses in JS?

== and != do exist in JS, but they have a famous historical pitfall: they silently convert types, leading to absurd results:

0 == "0"     // true (converts string to number before comparing)
0 == ""      // true (empty string also converts to 0!)
0 == false   // true
[] == ""     // true (empty array equals empty string!)

To fix this design flaw, JS later added === and !== (strict equality):

The only remaining legitimate use for ==:

Writing if (x == null) catches both null and undefined (because null == undefined is true); use === in all other scenarios.

4. Functions and Async

// Normal function (parameters can have default values)
function add(x, y = 1) {
  return x + y
}

// Arrow function (shorthand, like Lambda)
const add2 = (x, y) => x + y

// Async/await (add await before time-consuming operations like popups, network requests, same as coroutine suspend)
const res = await alert.present()

Question: Why call it an "arrow function" instead of Lambda? Is Lambda specific to Java/Kotlin?

Comments

Top 1 of 3 from juejin.cn, machine-translated. The original thread is authoritative.

2026再出发

Isn't iOS development just Swift?

Vic_wkx

Yes, but Swift feels a bit too heavy for me. I'm only learning iOS development to make an offline music player for my iPhone, and it looks like I can just use JS + Scriptable.

Vic_wkx

I recall the signing required to install a native iOS app comes with a lot of restrictions — it's not easy to deal with.