Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
console.log(+'100'); // 100
console.log(+null); // 0
console.log(+void 0); // NaN
console.log(+true); // 1
console.log(+false); // 0
console.log(+[]); // 0
console.log(+[100]); // 100
console.log(100 + ''); // "100"
console.log(null + ''); // "null"
console.log(void 0 + ''); // "undefined"
console.log(true + ''); // "true"
console.log(false + ''); // "false"
console.log([] + ''); // ""
console.log([100] + ''); // "100"
// METHOD 1: Using if/else statements
if (loggedIn) {
showUserProfile();
} else {
showLoginForm();
}
// METHOD 2: Using ternary operation
loggedIn ? showUserProfile() : showLoginForm();
var MIN_VALUE = 1;
var MAX_VALUE = 5;
/**
* boundedValue(val)
*
* Returns val if val between MIN_VALUE and MAX_VALUE both inclusive
*
* Otherwise, returns MIN_VALUE if val is less than MIN_VALUE
*
function randomInteger(maxvalue) {
return 1 + Math.floor(Math.random() * maxvalue);
}
var MIN_VALUE = 1;
var MAX_VALUE = 5;
/**
* boundedValue(val)
*
* Returns val if val between MIN_VALUE and MAX_VALUE both inclusive
*
* Otherwise, returns MIN_VALUE if val is less than MIN_VALUE
*
function leftZeroPad(value, maxlength) {
while (value.length < maxlength) {
value = '0' + value;
}
return value;
}
function boundedValue(minvalue, maxvalue) {
return function(value) {
return Math.max(minvalue, Math.min(maxvalue, +value || 0));
$('#button').on('click', function(evt) {
evt.preventDefault();
var content = $('<h1>Random Post Title</h1><p>Random post text.</p>');
$('#element').append(content);
});
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.toggleInputCase = this.toggleInputCase.bind(this);
this.state = { uppercase: false };
}
toggleInputCase() {
const isUpper = this.state.uppercase;
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.toggleInputCase = this.toggleInputCase.bind(this);
this.state = { uppercase: false };
}
toggleInputCase() {
const isUpper = this.state.uppercase;