Data types and objects in javascript

Updated: Sep 20

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



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




  • 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.




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 one of our partner companies, helping clients to solve business and technology challenges one line of code at a time.

4 views0 comments