Showing posts with label Primitive Types. Show all posts
Showing posts with label Primitive Types. Show all posts

Friday, November 17, 2017

Javascript: Identifying Primitive and Reference Types


Identifying Primitive Types

There are five primitive types in JavaScript:
  • boolean
  • number
  • string
  • null
  • undefined

The best way to identify primitive types is with the typeof operator, which works on any variable and returns a string indicating the type of data.
console.log(typeof "John Doe");   // "string"
console.log(typeof 10);           // "number"
console.log(typeof 5.1);          // "number"
console.log(typeof true);         // "boolean"
console.log(typeof undefined);    // "undefined"

When you run typeof null, the result is "object". The best way to determine if a value is null is to compare it against null directly, like below. Make sure that when you’re trying to identify null, use triple equals so that you can correctly identify the type.
console.log(value === null);    // true or false

Identifying Reference Types

The built-in reference types are
  • Array
  • Date
  • Error
  • Function
  • Object
  • RegExp

A function is the easiest reference type to identify because when you use the typeof operator on a function, the operator should return "function":
function reflect(value) {
 return value;
}
console.log(typeof reflect);   // "function"

Other reference types are trickier to identify because, for all reference types other than functions, typeof returns "object". To identify reference types more easily, you can use JavaScript’s instanceof operator. The instanceof operator takes an object and a constructor as parameters. When the value is an instance of the type that the constructor specifies, instanceof returns true; otherwise, it returns false.
var items = [];
var object = {};

function reflect(value) {
 return value;
}
console.log(items instanceof Array);    // true
console.log(object instanceof Object);    // true
console.log(reflect instanceof Function);   // true

Array.isArray() is the best way to identify arrays.
var items = [];
console.log(Array.isArray(items));    // true

Share This:    Facebook Twitter

Total Pageviews

My Social Profiles

View Sonal's profile on LinkedIn

Tags

__proto__ $Browser Access Grants Accessor properties Admin Ajax AllowsCallouts Apex Apex Map Apex Sharing AssignmentRuleHeader AsyncApexJob Asynchronous Auth Provider AWS Callbacks Connected app constructor Cookie CPU Time CSP Trusted Sites CSS Custom settings CustomLabels Data properties Database.Batchable Database.BatchableContext Database.query Describe Result Destructuring Dynamic Apex Dynamic SOQL Einstein Analytics enqueueJob Enterprise Territory Management Enumeration escapeSingleQuotes featured Flows geolocation getGlobalDescribe getOrgDefaults() getPicklistValues getRecordTypeId() getRecordTypeInfosByName() getURLParameters Google Maps Governor Limits hasOwnProperty() Heap Heap Size IIFE Immediately Invoked Function Expression Interview questions isCustom() Javascript Javascript Array jsForce Lightning Lightning Components Lightning Events lightning-record-edit-form lightning:combobox lightning:icon lightning:input lightning:select LockerService Lookup LWC Manual Sharing Map Modal Module Pattern Named Credentials NodeJS OAuth Object.freeze() Object.keys() Object.preventExtensions() Object.seal() Organization Wide Defaults Override PDF Reader Performance performance.now() Permission Sets Picklist Platform events Popup Postman Primitive Types Profiles Promise propertyIsEnumerable() prototype Query Selectivity Queueable Record types Reference Types Regex Regular Expressions Relationships Rest API Rest Operator Revealing Module Pattern Role Hierarchy Salesforce Salesforce Security Schema.DescribeFieldResult Schema.DescribeSObjectResult Schema.PicklistEntry Schema.SObjectField Schema.SObjectType Security Service Components Shadow DOM Sharing Sharing Rules Singleton Slots SOAP API SOAP Web Services SOQL SOQL injection Spread Operator Star Rating stripInaccessible svg svgIcon Synchronous this Token Triggers uiObjectInfoApi Upload Files VSCode Web Services XHR
Scroll To Top