Bu örnekte, bir nesnede anahtar olup olmadığını kontrol eden bir JavaScript programı yazmayı öğreneceksiniz.
Bu örneği anlamak için, aşağıdaki JavaScript programlama konuları hakkında bilgi sahibi olmalısınız:
- JavaScript Nesneleri
- JavaScript Nesnesi hasOwnProperty ()
Örnek 1: Operatörde Nesne Kullanımında Anahtarın Var olup olmadığını kontrol edin
// program to check if a key exists const person = ( id: 1, name: 'John', age: 23 ) // check if key exists const hasKey = 'name' in person; if(hasKey) ( console.log('The key exists.'); ) else ( console.log('The key does not exist.'); )
Çıktı
Anahtar var.
Yukarıdaki programda, inoperatör bir nesnede bir anahtar olup olmadığını kontrol etmek için kullanılır. inOperatör döner truebelirtilen anahtar aksi halde döner, nesne ise false.
Örnek 2: hasOwnProperty () Kullanan Nesnede Anahtarın Var olup olmadığını kontrol edin
// program to check if a key exists const person = ( id: 1, name: 'John', age: 23 ) //check if key exists const hasKey = person.hasOwnProperty('name'); if(hasKey) ( console.log('The key exists.'); ) else ( console.log('The key does not exist.'); )
Çıktı
Anahtar var.
Yukarıdaki programda, hasOwnProperty()yöntem, bir nesnede bir anahtar olup olmadığını kontrol etmek için kullanılır. hasOwnProperty()Yöntemi döndürür truebelirtilen anahtar aksi halde döner, nesne ise false.








