Type of Undeclared Variable in JavaScript: What is it?

 

Have you ever thought, what is type of undeclared variable in JavaScript? I know, the first thing that might come to mind is: how can an undeclared variable have a type? Yes, in JavaScript it is possible.

To understand it, let us start with understanding types in JavaScript. There are seven built in types in JavaScript. They are as follows:

  1. null
  2. undefined
  3. boolean
  4. number
  5. string
  6. object
  7. symbol (added on ES6)

Each variable with assigned value has a type. Let us consider the code listed below:

As you can see in the above snippet, if there is no value assigned then type of variable is undefined.

So far so good, we saw that variable with no assigned value is undefined.  Let us consider the next code snippet:

We have created a variable koo and have not assigned any value to it.  Now both value and type of koo are set to undefined.

Read full article on the Infragistics blog

Presented in ng-India 2019

ng-India is India’s largest conference on Angular and JavaScript. It happened on 23 Feb in Gurgaon, India and was attended by around 350 developers around India. There were speakers from all across world teaching on various topics such as Ivy rendering, Webpack, RxJS, Change Detection in Angular, Directives, ViewEncapsulation, ngRx etc.

I also had an opportunity to deliver a 30 min talk on “You do not know about JavaScript object “. You can find recording of my talk here: https://www.youtube.com/watch?v=h9eW0zSiFaw&t=9s

 

I also did welcome note for the conference, which can be find here:

https://www.youtube.com/watch?v=1CEWDQyCrmc&t=1s

 

You can find all talks from ng-India 2019, on geek97 YouTube channel

https://www.youtube.com/channel/UC9qYCj-KbQYJ_1fmnuULr4Q/videos

 

Thanks for watching my talk. Let me know your feedback.

Video – Arrow Function in JavaScript

Arrow function was introduced in ECMA 6.  It is different than function statement or expression , as it does not have its own ‘this’ , and can also not be used as constructor.  Some important points about Arrow function are:

  • It does not have prototype property
  • It does not have arguments parameter
  • It does not have its own ‘this’
  • It can not be used with new operator

I have written a detailed article on Arrow function here :

https://www.infragistics.com/community/blogs/b/infragistics/posts/easy-javascript-part-6-arrow-functions-in-javascript

Watch this video to learn more about Arrow function

 

Please do  not forget to subscribe  geek97 YouTube channel geek97 YouTube channel for update about future videos.

 

Recap of 2018

Hi Reader,

THANK YOU, for being with me in 2018. It was another good year due to your Support, Trust, and Love. Besides, you, I would like to thank my employer Infragistics and my boss Jason Beres that they gave me opportunities to contribute to developer community.

If you are an Angular developer, you may want to check fastest native Angular component library Ignite UI for Angular.

In 2018, I wrote articles, delivered talks in small meetups or big conferences, organized conference of 400 developers.

In 2018, I mainly focused on JavaScript and Angular. I was able to do the following:

Organizing ng-India was a kind of learning yet motivating experience. Since this was first ever-Angular specific conference in India, I had my doubts. However, everything went as planned. We sold all 300 tickets in less than one month and was joined by speakers from all across India. From printing goodies, printing T-Shirts, doing social media, to receiving speakers at AirPort, to delivering two talks, I had fun in almost everything in ng-India.

See Photos of ng-India here

I must thank all sponsors, volunteers, and speakers of ng-India. Without them, it was never possible. A special thanks to Shivprasad Koirala  and   Questpond for their trust in ng-India. Yes, finally yet importantly a big thank to vibrant Angular community for all motivation.

I delivered 11 talks in 2018. To deliver these talks, I travelled to six Indian cities and a country Nepal. Out of 11 talks, five talks were in big conferences such as ng-India, ng-Nepal, Pune Developer Conference and five talks were kind of workshops hosted by me in various cities. I also did one webinar for ng-Frankfurt.

I wrote 28 articles mainly on Angular and JavaScript and wrote an Angular Essential E-Book. I hope, I was useful to you in 2018.

Also in 2018, I got Microsoft MVP Award, which was ninth MVP Award for me since 2010. I have been getting it every year since 2010. I also had opportunity to attend MVP Summit in Seattle.

If you are reading my blog since 2010 and wondering, how I look on 31 December 2018, below photo is taken today

 

With all happiness, I wish you Happy New Year 2019. Keep learning, writing, teaching, and implementing. I am very excited for 2019.  Happy Coding.

Thanks with Regards

Dhananjay Kumar

Simplifying Object.assign method in JavaScript

Read full article on the Infragistics blog

“In JavaScript, Object’s assign method copies source object’s own enumerable properties to a target object, and returns that target object “

There are two important keywords in the above sentence:

  1. Enumerable
  2. Own

Before we go ahead and understand purpose of Object.assign, it is essential that we really understand these two words, enumerable properties and own properties.  Let us see them one by one:

A JavaScript object could have either enumerable or non-enumerable properties. However, by default when you create property on an object, it is enumerable.  Enumerable  means you can enumerate through those properties. Let us understand it through code.

 

 

There are two properties in babycat object. By default both are enumerable, hence as the output of for..in loop, you will get both age and name printed.

Now let us change default enumerable behavior of age property using Object.defineProperty method.

 

We have changed enumerable of age property to false, so as output, you will get only color printed. Hence, age is no longer an enumerable property of babycat object.

Another keyword in above sentence is own properties. To understand it, you need to understand object prototype chain. All JavaScript objects are part of a prototype chain, hence can access properties of its prototype’s also.  So, own properties are those properties, which are particular to the object and not from the prototype chain.  Let us understand own properties through code examples,

In above code snippet, there are two objects cat and babycat. In addition, [[Prototype]] property of babycat object is set to cat object. When you print properties of the babycat object using for..in loop, you will find as output age, color, name printed as shown in below image:

What is happening here? Well, JavaScript prints all properties from the prototype chain. However, only age and color are, own properties of babycat object.

As of now, you should have understood own properties and enumerable properties in context of a JavaScript object.   So let us revisit first statement of this post,

“In JavaScript, Object’s assign method copies source object’s own enumerable properties to a target object, and returns that target object “

You should able to infer what exactly above sentence implies.  Consider code below :

 

 

 

 

 

Using Object.assign() method , we are copying cat object own enumerable properties to babycat object. Here, cat object is source and babycat object is target. You will get output printed as below:

The Object.assign() method uses [[Get]] on the source object and [[set]] on the target object and invokes setter and getter to perform the task. Essentially, it assigns properties values from source to target object. It does not create new property in the target object.

As of now you know purpose of the Object.assign() method. Let us examine some variations while copying properties from source object to a target object.

Same Properties in both target and source object

If target object has same properties as of source object, then Object.assign() method will  override target object properties.  To understand it consider code listing below:

 

 

 

 

There is age property in both target object and source object.  While copying properties in target object the Object.assign() method will override target object age property, hence you will get output as shown in the below image :

Read full article on the Infragistics blog

Video – JavaScript object instances does not copy definition from the class

I recently wrote a blog post about Two Problems of a JavaScript Class. In my opinion one gotchas in a JavaScript class is , object instances does not copy definition from the class.

Watch this video to understand it through codes.  f you like the video , do not forget to subscribe to our YouTube Channel for notification about future video tutorials.

 

Two Problems of a JavaScript Class

 

Starting ECMA 6, JavaScript has class keyword to create a class. I have written in detail about class here. There is no second question that class simplifies the way object is created, inheritance is implemented etc. JavaScript class has,

  • Constructors
  • Methods
  • Extends etc.

Above features of a class helps to easily write Object Oriented JavaScript. As a developer, you do not need to know complexities of prototype chains, relationship between function constructor and its prototype object, and value of object’s __proto__ properties etc. to write effective Object Oriented JavaScript.  So, class keyword  is good addition to JavaScript language, however it is not gospel perfect. It has some problems, which may restrict you to write full-fledged Object Oriented JavaScript. In this post, I am going to share two such problems. Read along,

No Static Member Properties in class

A static member property is shared by all object instance of the class. JavaScript class does not allow creating it inside the class.

You cannot declare properties directly in the class. You can only have it through class’s constructors, and properties created inside constructor is local to the object instances and not shared by all of them.

Above code will throw error “unexpected identifier”.  There is a work around to create static property using the class prototype.

Now on the instances of Speaker class, you can access static property count.

Therefore, you are able to create a static property, but not without help of understanding of prototype.  In my opinion class should have a way to create static property directly inside the class like a method or constructor.

Object instances does not copy definitions from class

To understand this problem, let us first revise Constructor Invocation Pattern and prototype chain. You have a function constructor Speaker.

Using new operator, you can create object instances,

In this approach, a and b object instances both have their own copy of hello method.  Now if you add a hello method to Speaker.prototype, still a and b object instances will access their own copy of hello method. Consider below code:

Read full article on the Infragistics blog