Syntax Comparison and Mapping
Learn how JavaScript syntax maps to Kotlin syntax, including variables, functions, control structures, and collections
Syntax Comparison and Mapping
Welcome to the first module of JavaScript to Kotlin conversion! In this module, we'll explore how JavaScript syntax maps to Kotlin syntax, helping you understand the fundamental differences and similarities between these two languages.
Learning Objectives
By the end of this module, you'll be able to:
- Understand how JavaScript variable declarations map to Kotlin
- Convert JavaScript functions to Kotlin functions
- Map JavaScript control structures to Kotlin equivalents
- Work with Kotlin's type system and null safety
- Use Kotlin collections effectively
- Apply Kotlin's concise syntax patterns
Variable Declarations and Type System
JavaScript Variables vs Kotlin Variables
In JavaScript, you use let, const, and var for variable declarations. Kotlin has a more explicit type system with val (immutable) and var (mutable).
Key Differences
- Immutability:
valin Kotlin is likeconstin JavaScript, butvalcan be assigned once at runtime - Type Inference: Kotlin can infer types, but you can also specify them explicitly
- Null Safety: Kotlin has built-in null safety, while JavaScript doesn't
Function Declarations
JavaScript Functions vs Kotlin Functions
JavaScript has multiple ways to declare functions, while Kotlin has a more consistent approach.
Function Overloading
Kotlin supports function overloading, which JavaScript doesn't have natively.
Control Structures
If Statements
Both languages have similar if statements, but Kotlin's if is an expression.
When Expression (Kotlin) vs Switch Statement (JavaScript)
Kotlin's when is much more powerful than JavaScript's switch.
Loops
Kotlin has more concise loop syntax than JavaScript.
Collections
Arrays and Lists
JavaScript arrays are more flexible, while Kotlin has different collection types for different use cases.
Maps and Objects
JavaScript objects are similar to Kotlin maps, but with different syntax.
String Templates and Interpolation
Kotlin's string templates are more powerful than JavaScript's template literals.
Practice Exercises
Exercise 1: Convert JavaScript Function to Kotlin
Convert this JavaScript function to Kotlin:
function calculateTotal(items) {let total = 0;for (let item of items) {total += item.price * item.quantity;}return total;}
Exercise 2: Convert JavaScript Array Operations to Kotlin
Convert these JavaScript array operations to Kotlin:
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];const evenSquares = numbers.filter(n => n % 2 === 0).map(n => n * n).reduce((sum, n) => sum + n, 0);
Key Takeaways
- Type Safety: Kotlin provides compile-time type checking, while JavaScript is dynamically typed
- Null Safety: Kotlin's null safety prevents null pointer exceptions at compile time
- Immutability: Use
valfor immutable variables andvarfor mutable ones - Expression-based: Kotlin's
ifandwhenare expressions, not just statements - Collections: Kotlin has different collection types for different use cases
- String Templates: Kotlin's string interpolation is more powerful than JavaScript's
- Function Overloading: Kotlin supports function overloading, JavaScript doesn't
Next Steps
In the next module, we'll explore the JVM ecosystem and toolchain, including:
- Understanding the JVM platform
- Working with Gradle build system
- Package management and dependencies
- IDE integration and debugging tools
This foundation in syntax comparison will help you understand how to translate your JavaScript knowledge to Kotlin effectively.
Getting Started with the Kotlin Ecosystem
Understand the Kotlin ecosystem from a JavaScript developer's perspective, mastering core tools like Gradle, IntelliJ IDEA, and coroutines.
JVM Ecosystem and Toolchain
Learn about the JVM platform, Gradle build system, package management, and development tools for Kotlin development