Learn indexof( ) and hasOwnProperty( ) Operations in JavaScript

Learn indexof( ) and hasOwnProperty( ) Operations in JavaScript

SURYA L's photo
SURYA L
·Jul 11, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What is indexOf( )?
  • What is hasOwnProperty( ) in JavaScript ?
  • Conclusion:

Hello everyone hope you are all doing well, My name is Surya L.

In this blog we will Learn indexof and hasOwnProperty Operations in JavaScript.

What is indexOf( )?

indexOf is an JavaScript Operation which is used to returns the first index at which a given element can be found in the array, or -1 if it is not present.

  • Syntax:
arrrayName.indexOf(elementName);

Example for indexOf

const names=["Apple","Bat","Cat","Dog","Elephant","Frog","Giraffe","Zebra"];
//indexof is used to find the index of the element in the array
console.log("Output :",names.indexOf("Elephant"));
//returns 4 as array starts from 0
  • Output would be 4

What is hasOwnProperty( ) in JavaScript ?

If an object owns a property (rather than inheriting), hasOwnProperty() returns a boolean. It returns true if the object has that property, false otherwise.

Example for hasOwnProperty( )

let people={
    John: {
        age: 20,
        job: 'teacher'
    },
    Jane: {
        age: 21,
        job: 'designer'
    },
    Joe: {
        age: 22,
        job: 'driver'
    }
    ,
    Karen: {
        age: 23,
        job: 'doctor'
    }
}
console.log(people.hasOwnProperty('John'));
//Would return as true as John is a property of people object
console.log(people.hasOwnProperty('James'));
//Would return as false as James is a property of people object

Output:

  • people.hasOwnProperty('John')) Would return as true as John is a property of people object.

  • people.hasOwnProperty('James')) Would return as false as James is not a property of people object.

Conclusion:

  • indexOf is an JavaScript Operation which is used to returns the first index at which a given element can be found in the array, or -1 if it is not present.

  • If an object owns a property (rather than inheriting), hasOwnProperty() returns a boolean. It returns true if the object has that property, false otherwise.

Thanks for reading the blog. Do let me know what you think about it.

Reference: I learned this topic from freecodeCamp which i have minified. You can find me at Showwcase, Linkedin , Twitter , GitHub , Email .

Did you find this article valuable?

Support SURYA L by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this