© 2026 LinearBytes Inc.
Search posts, tags, users, and pages
Abhishek Singh
Learner
Nice observation. Good. But what about default case?
Biplab Malakar
Senior Software Engineer, JavaScript Developer, MEAN Developer, Node.js Developer, MERN Developer, Hybrid Mobile App Developer and ML Develo
Thats the issue, you can't define any default case. It will return 'undefined' if case not found. Or, you can do
function getVal(key){ return { 'a': 1, 'b': 2 }[key] || -1 }
getVal('c') // -1
if key not found then always return -1