Today I learned

A better Hello World program

The "Hello world" program is usually the first introduction to any programming language. It demonstrates the minimum amount you need to write a C program. It looks like this in the C programming language:

/* hello.c */
#import <stdio.h>

int main(int argc, char *argv[]) {
  printf("Hello, world!");
  return 0;

A Hello World program in C. It demonstrates the minimum amount you need to write a C program.

A modern "Hello world"

In more modern languages however, this example isn't as useful anymore. Here's the same example in Python. Notice how it doesn't teach anything other than print.

print "Hello, world!"

A Hello World program in Python. There's barely anything in it!

"Hello world" in JavaScript

In today's world of more succint programming languages, we need a different "hello world" to demonstrate language features better. Here's what I propose:

function getGreeting(name) {
  return `Hello, ${name}!`

const message = getGreeting('world')

A "better" Hello World program in JavaScript, showing more basic functionality than just printing lines.

How is this better?

This simple example demonstrates a few more things than printing strings, such as:

function getGreeting(name) {
  return /*...*/

Functions: How to write a function with an argument, and returning values from functions. Also shows the naming convention for functions (camelCase).

const message = /*...*/

Variables: How to assign variables.

;`Hello, ${name}!`

Strings: Dealing with basic string functions.

Example: Go version

I've started writing these kinds of programs for languages that I'm learning. Here's how it'd look like in Go, which I've added to my Go cheatsheet:

// hello.go
package main

import "fmt"

func main() {
  message := getGreeting("world")

func getGreeting(name string) (string) {
  return "Hello, " + name + "!"

Example: Elixir version

Here's an Elixir version, also at the Elixir cheatsheet:

# hello.exs
defmodule Greeter do
  def get_greeting(name) do
    "Hello, " <> name <> "!"

message = Greeter.get_greeting("world")
IO.puts message

You have just read A better Hello World program, written on September 19, 2017. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles