Firstly sorry for such vague question, I couldn't think of title which make sense.
I was trying to understand this line in JS (outputs: 12216520)
color = +("0x" + color.slice(1).replace(
color.length < 5 && /./g, '$&$&'));
Can someone help me in understanding that how javascript is going to evaluate it?
I tried two snippet
color = +`0x${color}`.slice(1).replace(
color.length < 5 && /./g, '$&$&');
This gives null in console
and
color = "0x" + color.slice(1).replace(
color.length < 5 && /./g, '$&$&') + color;
This gives me 0xBA68C8 #BA68C8 in console
Whereas the first snippet outputs this 12216520
Code snippet for reference
console.log(lightOrDark('#BA68C8 '))
function lightOrDark(color) {
var r, g, b, hsp;
let co
if (color.match(/^rgb/)) {
color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/);
r = parseInt(color[1]);
g = parseInt(color[2]);
b = parseInt(color[3]);
}
else {
color = +("0x" + color.slice(1).replace(
color.length < 5 && /./g, '$&$&'));
r = color >> 16;
g = color >> 8 & 255;
b = color & 255;
}
hsp = Math.sqrt(
0.299 * (r * r) +
0.587 * (g * g) +
0.114 * (b * b)
);
if (hsp>150) {
return 'light';
}
else {
return 'dark';
}
}