jsrosetta

Basics

String Interpolation

How JavaScript's template literals (`${}`) compare to Go's fmt.Sprintf, Rust's format!/println!, Swift's string interpolation, and Java's String.formatted.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

JavaScript has string interpolation built right into the language via template literals (backtick strings). Go has no dedicated syntax for this — you use fmt.Sprintf with format verbs (%s, %d, …), similar to C's printf. Swift has real string interpolation built into the language: \(...) accepts any expression. Rust is different: format!/println! are just standard-library macros, not core language syntax; since Rust 1.58 they let you capture a plain identifier already in scope directly inside the braces ({name}), but they don't accept expressions or field access there — {user.name} or {a + b} simply won't compile, so you have to bind a local variable first or use positional/named arguments (format!("{}", user.name)). Java has no string interpolation at all — even the String Templates feature that was previewed in Java 21/22 was withdrawn — so it's still String.format/.formatted() in the printf style, just like Go.

String interpolation

const name = 'bob'
const age = 21
const message = `${name} is ${age} years old`
 
console.log(message)
bob is 21 years old

Reference: github.com/miguelmota/golang-for-nodejs-developers#interpolation