Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
56: Scala.js for Frontend Development
When I first started chatting with developers about Scala.js, the most common thing I heard was: "Why bother? It's just a wrapper around JavaScript. You're basically writing JS with a Scala 'skin,' and you'll spend all your time fighting the compiler to make it behave like the browser wants."
That is fundamentally wrong. If Scala.js were just a skin, it would be a toy. In reality, Scala.js is a full-blown compiler that targets JavaScript, meaning you get the entire power of the Scala type system—generics, implicits, and high-order functions—running natively in the browser. You aren't "simulating" Scala; you are executing it. The magic isn't in how it wraps JS, but in how it allows you to define rigorous boundaries between your typed Scala logic and the untyped chaos of the DOM.
The Myth of the "JS-in-Scala" Wrapper
People think that to use a JavaScript library, you have to write clunky, boilerplate-heavy wrappers that feel like you're writing Java in 2005. They assume you lose the "Scala feel" the moment you touch the frontend. But let's look at how we actually handle an external JS object. Suppose you have a tiny JS utility in your project that handles user session data:
// session-util.js
export const sessionManager = {
saveSession: (userId, token) => {
localStorage.setItem('user_id', userId);
localStorage.setItem('auth_token', token);
},
getSession: () => ({
id: localStorage.getItem('user_id'),
token: localStorage.getItem('auth_token')
})
};
If this were just a "wrapper," you'd be casting things to Any and praying you didn't misspell a key. Instead, you define a facade. You tell Scala exactly what the JS object looks like, and from that point on, the compiler protects you.
Bridging the Gap with Type-Safe Interop
To interact with the code above, we don't "wrap" it in the traditional sense. We describe its interface. I prefer using js.Object and @JSImport to create a clean bridge. Here is how I would actually implement the interface for that session manager:
import scala.scalajs.js
import scala.scalajs.js.annotation._
@JSImport("./session-util.js", "sessionManager")
object SessionManager extends js.Object {
def saveSession(userId: String, token: String): js.Unit = js.native
def getSession(): SessionData = js.native
}
@js.native
trait SessionData extends js.Object {
val id: String
val token: String
}
Notice the js.native keyword. I'm not writing the implementation here; I'm telling the Scala compiler: "Trust me, this method exists in the JavaScript runtime, and it follows this signature." Now, in the rest of your application, you can call SessionManager.saveSession("123", "abc") with full type safety. If you try to pass an Integer where a String is expected, your code won't even compile. You've successfully brought the volatility of JavaScript under the control of the Scala type system.
I should mention that you aren't limited to just calling JS functions. You can use Scala's powerful collections and functional paradigms to manipulate data on the frontend, then pass the final result to the DOM. The real power of Scala.js is when you share code. If you have a case class User(id: String, email: String) in a shared module, you can use that exact same class in your backend (JVM) and your frontend (JS). No more duplicating DTOs or manually parsing JSON on both ends just to make sure the fields match.
📋 Practical Task
Implement a LocalStorage User Preference Store
Your task is to create a type-safe Scala.js interface for a browser-based preference manager. You need to bridge a JavaScript object that handles theme and language settings.
The JavaScript environment provides this object:
const prefStore = {
setPreference: (key, value) => {
localStorage.setItem(`pref_${key}`, value);
},
getPreference: (key) => {
return localStorage.getItem(`pref_${key}`);
}
};
Requirements:
- Create a
@JSImportobject namedPrefStorethat maps to theprefStoreJS object. - Define the
setPreferenceandgetPreferencemethods usingjs.native. - Ensure the methods use
Stringfor both keys and values. - Write a small Scala function
updateTheme(newTheme: String): Unitthat uses yourPrefStorefacade to save a theme preference under the key"theme".
There are no comments for now.