Skip to content

Instantly share code, notes, and snippets.

@jacekschae
Last active May 30, 2019 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacekschae/17992f9627f3e247ba46f62fa35fdaec to your computer and use it in GitHub Desktop.
Save jacekschae/17992f9627f3e247ba46f62fa35fdaec to your computer and use it in GitHub Desktop.
ClojureScript and JavaScript Comparison - JavaScript Examples
// Functions
function sum(x, y) {
return x + y;
}
// Shorthand Functions
const sum = (x, y) => x + y;
// Function Invocation
sum(3, 4)
// Anonymous Functions
function (x, y) {
return x + y;
}
// Anonymous Shorthand Functions
(x, y) => x + y;
// map
[1, 2, 3].map(num => num + 1)
// filter
[1, 2, 3].filter(num => num % 2)
// reduce
[1, 2, 3].reduce((acc, curr) => {
return acc + curr;
}, 0)
// if withouth else
if (lang === 'de') {
return 'german';
}
// if with eles
if (lang === 'de') {
return 'german';
} else {
return 'english';
}
// Ternary
lang === 'de'
? 'german'
: 'english'
// if with multiple else
function whichLang(lang) {
if (lang === 'de') {
return 'german';
} else if (lang === 'es') {
return 'spanish';
} else {
return 'english';
}
}
// switch
function whichLang(lang) {
switch (lang) {
case 'de':
return 'german';
case 'es':
return 'spanish';
default:
return 'english';
}
}
// Falsy
false
null
undefined
NaN
'' // empty string
0
-0
// Interop
// Method call
console.log("Hello")
// Property access
Math.PI
// Property setting
num.one = 1
// window method call
alert("Hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment