Skip to content

gmarcos87/switch-return-proposal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Switch return

The Problem

Nested if statements to define a constant

const colorIf = type === 'apple'
  ? 'red'
  : type == 'banana'
    ? 'yellow'
    : 'invisible'
const colorSwith = ((t) => {
  switch(t) {
    case 'apple':
      return 'red'
    case 'banana':
      return 'yellow'
    default
      return 'invisible'
  }
})(type)

Solutions

To be able to use switch as an expression to assign a value. It could even be used with the implicit return within the cases.

Syntax

const color = switch(type) {
  case 'apple':
    return 'red'
  case 'banana':
    return 'yellow'
  default
    return 'invisible'
}

With implicit return

const color = switch(type) {
  case 'apple':
    'red'
  case 'banana':
    'yellow'
  default
    'invisible'
}

About

Proposal for "switch" to return a value

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages