default values


Node.js

  1. function greet(name = 'stranger') {
  2. return `hello ${name}`
  3. }
  4. let message = greet()
  5. console.log(message)
  6. message = greet('bob')
  7. console.log(message)

Output

  1. hello stranger
  2. hello bob

Go

use pointers and check for nil to know if explicitly left blank

  1. package main
  2. import "fmt"
  3. func greet(name *string) string {
  4. n := "stranger"
  5. if name != nil {
  6. n = *name
  7. }
  8. return fmt.Sprintf("hello %s", n)
  9. }
  10. func main() {
  11. message := greet(nil)
  12. fmt.Println(message)
  13. name := "bob"
  14. message = greet(&name)
  15. fmt.Println(message)
  16. }

Output

  1. hello stranger
  2. hello bob