Types of Variables

Non-premitive data type

·

3 min read

As we all know , there are eight data types in JavaScript. Seven of them are called “primitive”, because their values contain only a single thing (be it a string or a number or whatever).

In contrast, objects are used to store keyed collections of various data and more complex entities. In JavaScript, an object is a collection of properties, it can hold multiple properties. So we must understand them first.

An object can be created with curly brackets {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything.

Different operations we can perform on objects :-

// Accessing a value
// Adding new property
// Updating a value
// Deleting the property

Rules for naming the key in an object :-

--> Remember when you having multiple word key name, put it in double quotes.

let userInfo = {
 age: 23,
"first name" : "nmncoded"
}

--> Whenever you are accessing the more than single word key the (Dot Operator) will not work.

user.first name   / / unexpected indentifier
user["first name"]   / /  will give result

--> Whenever you are accessing the numbers key the (Dot Operator) will not work.

user.10   / / unexpected indentifier
user[10]   / /  will give result

--> Functions inside objects are called methods. To access the object a method can use the (this) keyword.

let userInfo = {
 age: 23,
"name" : "nmncoded",
sayHi(){
 alert("this.name")  / /  "this" is the current object
  }
};
userInfo.sayHi();  / /  nmncoded.

--> Use [] for computed property --> Because square brackets does two things. It first compute the value, then access the value.

--> Whenever you are looking for a property that doesn't exist in an object. It will always returned ( undefined ).

let keyValue = " username ";
let character = {
  username : "nmncoded",
};
console.log( character ["keyValue"] )  //  Undefined. Because KeyValue is a string here.
console.log( character [keyValue] )  //  nmncoded. Because KeyValue is a variable here.

--> When there is a variable name, number, space between and any special character, we have to use square brackets. Otherwise we will use ( dot notation ). Square brackets are much more powerful than the dot notation. They allow any property names and variables. But they are also more cumbersome to write.

So most of the time, when property names are known and simple, the dot is used. And if we need something more complex, then we switch to square brackets.

let fruitName = 'kiwi';
let bag = {
  [fruitName + 'fruit']: 5 // bag.kiwiFruit = 5
};

Property names limitations

As we already know, a variable cannot have a name equal to one of language-reserved words like “for”, “let”, “return” etc.

But for an object property, there’s no such restriction:

// these properties are all right
let obj = {
  for: 0,
  let: 1,
  return: 2
};

alert( obj.for + obj.let + obj.return );  // 3

In short, there are no limitations on property names. They can be any strings or symbols (a special type for identifiers, to be covered later).

Other types are automatically converted to strings.

There are many other kinds of objects in JavaScript:

-->Array to store ordered data collections.
-->Date to store the information about the date and time.
-->Error to store the information about an error. …And so on.

They have their special features that we’ll study later.