data types

Data types and Objects in JavaScript

Before we discuss objects, let’s go through data types. We have 7 data types.

Data Types

Primitive

  • Numbers — const age = 23
  • Undefined — const x
  • Strings (Text) — const a = “init”
  • Null – The value null represents the intentional absence of any object value.
  • Booleans (True or false statements) — const c = true
  • Symbol – Searches for existing Symbols with the given key and returns it if found. Otherwise, a new Symbol gets created in the global Symbol registry with key.

Non-Primitive data types

  • Objects (for more complex data structures) — const name = {firstName:”John”, lastName:”Doe”}

Difference between Primitive and Non-Primitive data type:

Primitive data types are not mutable.

JavaScript Primitive data types are data types that refer to a single value.

var a = 5;

When we create a variable, it reserves a space for itself in the memory. The variable ‘a’ has space in memory that holds its value. When we try to change the value of ‘a’ by assigning another value like var a = 6, it doesn’t alter the value of the original a, it just creates a new variable ‘a’ with the new value 6.

Everything out of primitive data types are objects in JavaScript.

Objects

It is used to store various keyed collections and more complex entities.

A simple variable

let car = “Fiat”;

Objects are variables too, however, objects contain multiple values

const person = {  
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};
  • We can define a JavaScript object with an object literal: { }
  • It is also common practice to define objects with const
  • An object in JavaScript contains key-value pairs in its address. When we refer to person, we are referring to the address in memory that contains {firstName:  , lastName … etc} instead of the values directly.

Object Properties

The key in our key: values pairs in JavaScript objects are called object properties, the values are property values.

We can access Object Properties through dot notation:

ObjectName.propertyName

Objects can also have methods.

Methods are actions that can be performed on objects. They are stored in properties as function definitions.

const person = {  
  firstName: "John",
  lastName : "Doe",
  id: 5566,  
  fullName : function() {
  return this.firstName + " " + this.lastName;
  }
};

In a function definition, this refers to the “owner” of the function.

In the example above, this is the person object that “owns” the fullName function.

In other words, this.firstName means the firstName property of this object.

We access Object methods similarly with objectName.methodName( )

An important application of objects in Javascript would be JSON Objects.

JSON stands for JavaScript Object Notation. JSON is a text format for storing and transporting data whether you’re rendering data in your web applications or API calls from your database.

The JSON syntax is similar to the way of creating JavaScript objects. Due to this, a JavaScript program can easily convert JSON data into JavaScript objects.

‘Kash works as a full-stack software developer at WayMaker Digital, helping clients to solve business and technology challenges.

WayMaker Digital provides technology consulting, product design and information products, that enable our clients to consistently thrive and innovate to meet user needs. The quality of our experiences and ideas set us apart when it comes to service delivery, customer experience design and product development. 

Our team of consultants have built digital experiences for leading brands with complex business scenarios ranging from startup companies to public services and from telecoms to e-commerce; no project deliverable is too complex.

Our approach: we adopt human-centred & design thinking models to solve complex business challenges, which ensure our clients are on top of their business.

Do you have a question or an enquiry; you want to discuss a project or need a free consultation? Give us a shout here.

Share with Friends

Leave a Comment

Your email address will not be published.

Scroll to Top