Can the result of a For-In loop use undefined or null?

This is my code:

const a = function(obj) { for (let key in obj) { if (!obj.hasOwnProperty(key)) { continue; } console.info(key.split('_')); } }; a({a_b: 123}); 

I thought there were no problems, but SonarQube gives a critical error:

TypeError can be selected as a "key" can be null or undefined here.

Highlighted word in key.split ('_') . The specified variable key may be undefined / null here.

I tried passing something like {[undefined]: 123} , and the key variable will become the string "undefined" instead of the real undefined.

Hence. I am wondering if the key will be undefined / null in any possible situation? Or is it just falsely positive?

Here is a screenshot:

Picture

+5
source share
1 answer

This is a known bug in the SonarQube JavaScript analyzer, which was fixed several months ago. You must upgrade to the latest JavaScript plugin.

+8
source

Source: https://habr.com/ru/post/1259189/


All Articles