The presence of a jammer in an IoT network severely degrades all communication efforts between adjacent wireless devices. The situation is getting worse due to retransmission attempts made by affected devices. Therefore, jammers must be detected or localized quickly to activate a series of corrective countermeasures so as to ensure the robust operation of the IoT network. This paper proposes a novel metric called the number of jammed slots (NJS). It can detect and localize both reactive and proactive jammers that follow arbitrary jamming attack patterns. NJS is applicable to all communication paradigms such as unicast, broadcast, and multicast. In NJS, the wireless medium status is monitored by IoT devices and summarized reports are sent to a central node. Then, the central node determines the jamming duration, the affected nodes, and the approximate location of the jammer(s). Also, the specificity, precision, and accuracy of NJS are at least 48%, 19%, and 20% better than the other state-of-the-art statistical methods, respectively. In addition, in terms of the detection time, NJS is four times faster when detecting an active jammer in the network. It can also localize the jammer with less jammer localization errors.