How to check an undefined value?
In JavaScript we have different type of undefined values:
if (this.state.subject.id === undefined) {
console.log('this.state.subject.id: is undefined');
}
if (this.state.subject.id === null) {
console.log('this.state.subject.id: is null');
}
if (this.state.subject.id === "") {
console.log('this.state.subject.id: is empty');
}
Converting data type
From String to Integer
From String (with thousands separator) to Integer
var category = "8'100'000"
var categoryAsInteger = parseInt(category.replace(/'/g,""), 10);
Date and time operations
Get date time in YYYY-MM-dd hh:mm:SS format
var today = new Date();
var date = today.getFullYear().toString().substring(0) + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var timeStamp = date + ' ' + time;
console.log(`timeStamp: ${timeStamp}`)
JSON operations
Logging of JSON Data
The following examples show different ways to log a JSON object:
let jsonObject = '{"name":"Mike Dane","login":"mdane","password":"12345"}';
console.log(`JSON: ${jsonObject}`);
console.log(`JSON.stringify(): ${JSON.stringify(jsonObject)}`);
console.log(`JSON.parse(): ${JSON.parse(jsonObject).name}`);
Logs:
JSON: {"name":"Mike Dane","login":"mdane","password":"12345"}
JSON.stringify(): "{\"name\":\"Mike Dane\",\"login\":\"mdane\",\"password\":\"12345\"}"
JSON.parse(): Mike Dane