# Getting the Type of a Primitive

**URL:** <https://community.cesium.com/t/getting-the-type-of-a-primitive/682>\
**Category:** CesiumJS\
**Created:** [August 23, 2013, 4:41pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682 "2013-08-23T16:41:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bp39356](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bp39356](https://community.cesium.com/u/bp39356)\
**Post date:** [August 23, 2013, 4:41pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682/1 "2013-08-23T16:41:25Z")

</div>

Hello there,

I am wondering if it is possible to get the type of a primitive from the scene? For example, I am able to get the collection of all primitives in the scene, and I know that a billboardCollection is a type of primitive, as the following code works fine.

scene.getPrimitives().add(billboards);

(where billboards is a billboardCollection).

How can I know if a primitive is a billboardCollection, or any other type of primitive?

Here is a code snippet that shows how I'd like to do the check.

//Get all of the primitives in the scene (this includes billboardCollection(s)).  
var primitives = scene.getPrimitives();

//Loop for each primitive in the scene.  
for(var i = 0; i \< primitives.getLength(); i++)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;//Check if the primitive is a billboardCollection.  
&nbsp;&nbsp;&nbsp;&nbsp;//How can I get the type of the primitive? I'm looking for something like  
&nbsp;&nbsp;&nbsp;&nbsp;//Type = billboardCollection.

&nbsp;&nbsp;&nbsp;&nbsp;if(primitives.get(i).type == "billboardCollection")  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Do stuff if it is a billboardCollection.  
&nbsp;&nbsp;&nbsp;&nbsp;}

}

---

<div class="post-metadata">

**Author:** ![Scott\_Hunter](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_hunter/32/2006_2.png) [@Scott\_Hunter](https://community.cesium.com/u/Scott_Hunter)\
**Post date:** [August 23, 2013, 5:32pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682/2 "2013-08-23T17:32:29Z")

</div>

Determining the type of an object in JavaScript is sometimes difficult, due to how loose the type system can be. In this case, though, you could try comparing the constructor property of the primitive against BillboardCollection:

if (primitives.get(i).constructor === Cesium.BillboardCollection) {

---

<div class="post-metadata">

**Author:** ![Matt\_Amato](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/matt_amato/32/1993_2.png) [@Matt\_Amato](https://community.cesium.com/u/Matt_Amato)\
**Post date:** [August 23, 2013, 5:35pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682/3 "2013-08-23T17:35:05Z")

</div>

Scott, is the constructor notation preferred over

if (primitives.get(i) instanceof Cesium.BillboardCollection)

{

If so, why?

---

<div class="post-metadata">

**Author:** ![Scott\_Hunter](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/scott_hunter/32/2006_2.png) [@Scott\_Hunter](https://community.cesium.com/u/Scott_Hunter)\
**Post date:** [August 23, 2013, 5:46pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682/4 "2013-08-23T17:46:07Z")

</div>

Either will work. instanceof will walk up the prototype chain, whereas an object has only one constructor. So, for example:

var billboardCollection = new BillboardCollection();

billboardCollection instanceof BillboardCollection;

// ==\> true

billboardCollection instanceof Object;

// ==\> true

billboardCollection.constructor === BillboardCollection;

// ==\> true

billboardCollection.constructor === Object;

// ==\> false

In Cesium, we don’t use the prototype chain for inheritance, so for our types, they’re mostly equivalent. When I wrote my earlier email, I forgot about the instanceof operator.

---

<div class="post-metadata">

**Author:** ![bp39356](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bp39356](https://community.cesium.com/u/bp39356)\
**Post date:** [August 23, 2013, 6:43pm UTC](https://community.cesium.com/t/getting-the-type-of-a-primitive/682/5 "2013-08-23T18:43:05Z")

</div>

Ahh I see. Fantastic, this is exactly what I was looking for.

Thanks a lot for the response! and for the discussion comparing the constructor property vs the instanceof operator. Things are much more clear now.
