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

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

The same keyword but could be of different meaning Val, Var, Let, Const, Static, and Def across JavaScript, Swift, Kotlin, and Scala

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Top Mobile App Developers & Leading App Development Companies in UK

Profusely examined top Mobile App Development companies in UK with ratings & reviews to help find the best Mobile App Development solution providers.

Mobile App Development North Carolina

Mobile App Development North Carolina In the era of globalization, technology has forced the businesses and industries to jump into the space of competition. Technology has both tangible and intangible benefits that help businesses from different ind...

Top 7 Mobile App Development Companies in Delhi NCR

Looking for a Mobile app development company in Delhi NCR? Here it a list of top mobile app development companies in Delhi for Android & iOS app Development.

ECommerce Mobile App Development | Ecommerce Mobile App Development Services

We are leading ecommerce mobile application development company. Hire our ecommerce mobile app developer for your custom Ecommerce project at competitive rates. **Know about [Top ECommerce Mobile App Development...