Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.