You will find these keywords, valvarletconststatic, and defused across these languages to define a variable, i.e. JavaScript, Swift, and Scala, as seen below.

A simple thinking is that if the keyword is the same, then the behavior is the same across the languages. Unfortunately, they are of different meanings sometimes.

Hence I’m writing here to share about them for a quick comparison.

A rogue mutable variable

This is the var in JavaScript, that is unique to this “wild-wild west” language that can do almost anything. No other languages (above) has this attribute, except for JavaScript.

Below are some very interesting attributes it has.

Assign and Mutate (okay, this is not interesting)

// JavaScript
var x = 10
x = 11          // reassign to mutate the value
console.log(x)  // result 11

Recreate the variable (a no-no in the other languages)

// JavaScript
var x = 10
var x = 11      // this is okay
console.log(x)  // result 11

Accessibly outside the scope (a no-no in the other languages)

// JavaScript
function test() {
    {
        var x = 10
    }
    console.log(x)  // accessible and result is 10
}

Note, it can’t be accessed outside the function though. Therefore it is also called a function scoped variable.

Attached to the window if defined at global scope

This means if it is defined at the global scope, it will stick to the window

var stickyVariable = 10
window.stickyVariable    // available in window and result as 10

Using this type variable is pretty risky as it easily gets mutated and can affect and be affected by 3rd party libraries variable setting if the name collide. It is there as pre ES6, that’s the way JavaScript has its variable defined.

A normal mutable variable

When I term normal, I mean the life of the variable is limit within the scope where it is defined i.e. { /* scope is covered by curley brackets */ }.

A mutable variable is a variable that CAN be changed (reassigned) with a new value after being created.

  • In Swift, Kotlin, and Scala, they are named var.
  • However, in JavaScript, it is named let.

After a variable has been created, it cannot be created anymore

// JavaScript
let x = 10
let x = 11     // Error: Identifier 'x' has already been declared

However, it can be reassigned

// JavaScript
let x = 10
x = 11          // this is okay
console.log(x)  // result 11

A normal immutable variable

When I term normal, I mean the life of the variable is limit within the scope where it is defined i.e. { /* scope is covered by curley brackets */ }.

An immutable variable is a variable that CANNOT be changed (reassigned) with a new value after being created.

  • In Kotlin and Scala, they are named var.
  • In Swift, it is named as let (don’t mix up with the let in JavaScript).
  • In JavaScript, it is named as const
// JavaScript
const x = 10
x = 11        // Assignment to constant variable

#software-development #web-development #programming #software-engineering #mobile-app-development #mobile-app

Val, Var, Let, Const, Static, and Def across JavaScript, Swift, Kotlin, and Scala
1.40 GEEK