Side-by-side mappings comparing JavaScript and Elm.
This is intended to elaborate on the official Elm documentation From JavaScript? page
so make sure you check that out first if you haven't already.
JavaScript
Elm
===
==
!==
\=
<
<
<=
<=
>
>
>=
>=
JavaScript
Elm
const add = (a, b) => a + b
add a b = a + b
add(1, add(1,2)
add 1 (add 1 2)
const a = [ 1 , 2 ] ;
const b = [ 3 , 4 ] ;
const c = a . concat ( b ) ;
// or
const c = [ ...a , ...b ] ;
a = [ 1 , 2 ]
b = [ 3 , 4 ]
c = a ++ b
Comments
JavaScript
Elm
// single line comment
-- single line comment
/* multi line comment */
{- multi line comment -}
JavaScript
Elm
npm install
elm install
package.json
elm.json
Destructuring / Pattern Matching
JavaScript
Elm
let {name, age} = user;
{name, age} = user
({prop}) => prop
\{prop} -> prop
if ( true ) {
return "foo"
} else {
return "bar" ;
}
if True then " foo" else " bar"
-- or
if True then \
" foo"
else \
" bar"
Redux
Elm
action
msg
reducer
update
state
Model
function reducer ( state = 0 , action ) {
switch ( action . type ) {
case 'INCREMENT' :
return state + 1 ;
case 'DECREMENT' :
return state - 1 ;
default :
return state ;
}
}
-- MODEL
type alias Model = Int
model : Model
model = 0
-- UPDATE
type Msg = Increment | Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
Components / View Functions
const HelloWorld = ( props ) => (
< div style = { { color : 'hotpink' } } > Hello world!</ div >
) ;
helloWorld model =
div [ style " color" " hotpink" ] [ text " Hello world!" ]
this . setState ( { counter : 0 } ) ;