let Over var, === Over ==: The JavaScript Essentials Kotlin Devs Need
1. Variable Declarations (no type needed, semicolons optional)
const a = 10: immutable constant (maps to Kotlinval)let b = "hi": mutable variable (maps to Kotlinvar)- Two null-like values:
null(explicitly set to empty) andundefined(never assigned/doesn't exist)
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?
- JS actually does have
var(it does stand for variable), but it was designed early and has serious flaws (like ignoring{}block scope; variables declared insideiforforleak outside). - To remain compatible with old web pages, JS can't directly change the underlying logic of
var, soletwas added later as a fix. - Modern JS convention: completely deprecate
var; use onlyletfor mutable variables andconstfor constants.
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:
Method 1: Use
class(most similar to Kotlin/Java)class User { constructor(name, age) { this.name = name this.age = age } } const u1 = new User("Tom", 18) const u2 = new User("Jerry", 20)Method 2: Factory function (most common and easiest in small scripts)
function createUser(name, age) { return { name, age } // shorthand when property name and variable name are the same } const u1 = createUser("Tom", 18) const u2 = createUser("Jerry", 20)Method 3: Use TypeScript (standard for large projects)
Because pure JS doesn't force you to check whether you've missed or misspelled property names, in real projects people use TypeScript (a superset that adds a type system to JS), defining
interface Userfirst like in Kotlin, then compiling to plain JS to run.
3. Common Operators
===and!==: test equality / inequality (always use three equals, never==)condition ? value1 : value2: ternary operator (equivalent toif (condition) value1 else value2)obj?.prop: safe call (returnsundefinedwithout error when empty, same as Kotlin)a ?? 0: nullish coalescing (takes default value0whenais empty, same as Kotlin?:)
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):
- Rule: must have same type and same value to be
true(e.g.,0 === "0"is directlyfalse), which is the normal comparison logic in Kotlin/Java.
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?
- Lambda (λ) is a universal mathematical concept in programming languages (Python, C++, C#, Java, Kotlin all have it).
- In JS official docs, because anonymous functions
function(x, y) {}already existed, when(x, y) => x + ywas added later, the official name was based on its appearance: Arrow Function.
Top 1 of 3 from juejin.cn, machine-translated. The original thread is authoritative.
Isn't iOS development just Swift?
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.
I recall the signing required to install a native iOS app comes with a lot of restrictions — it's not easy to deal with.