langShiftlangShift

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).

Loading editor...

Key Differences

  1. Immutability: val in Kotlin is like const in JavaScript, but val can be assigned once at runtime
  2. Type Inference: Kotlin can infer types, but you can also specify them explicitly
  3. Null Safety: Kotlin has built-in null safety, while JavaScript doesn't
Loading editor...

Function Declarations

JavaScript Functions vs Kotlin Functions

JavaScript has multiple ways to declare functions, while Kotlin has a more consistent approach.

Loading editor...

Function Overloading

Kotlin supports function overloading, which JavaScript doesn't have natively.

Loading editor...

Control Structures

If Statements

Both languages have similar if statements, but Kotlin's if is an expression.

Loading editor...

When Expression (Kotlin) vs Switch Statement (JavaScript)

Kotlin's when is much more powerful than JavaScript's switch.

Loading editor...

Loops

Kotlin has more concise loop syntax than JavaScript.

Loading editor...

Collections

Arrays and Lists

JavaScript arrays are more flexible, while Kotlin has different collection types for different use cases.

Loading editor...

Maps and Objects

JavaScript objects are similar to Kotlin maps, but with different syntax.

Loading editor...

String Templates and Interpolation

Kotlin's string templates are more powerful than JavaScript's template literals.

Loading editor...

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;
}
Loading editor...

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);
Loading editor...

Key Takeaways

  1. Type Safety: Kotlin provides compile-time type checking, while JavaScript is dynamically typed
  2. Null Safety: Kotlin's null safety prevents null pointer exceptions at compile time
  3. Immutability: Use val for immutable variables and var for mutable ones
  4. Expression-based: Kotlin's if and when are expressions, not just statements
  5. Collections: Kotlin has different collection types for different use cases
  6. String Templates: Kotlin's string interpolation is more powerful than JavaScript's
  7. 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.