I am writing a function which returns type and value in the format ‘number=2’ (for example). It works fine for some data types, but when it comes to strings and arrays, the quote marks (for strings) and the square brackets (for arrays) are missing (I need them to be shown).
Does someone know how I might be able to include these in the output please?
function typing(param) {
let varType = typeof param;
return(`${varType}=${param}`);
}
you can specify conditions for your parameters like so,
function typing(param) {
let varType = typeof param;
if(varType=="string"){
param = `"${param}"`;
}
else if(varType=="object"){
// if you want to specify that array parameter shows up as array type, you can
// use this line written next to the sentence or ignore it
varType = "array";
// console.log(typeof Array); // returns object type
param = `[${param}]`
}
return(`${varType}=${param}`);
}