<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript In Simple Language &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/javascript-in-simple-language/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 07:45:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>JavaScript In Simple Language &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Do you know JavaScript? Are you sure? &#8211; Part Two</title>
		<link>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-two/</link>
					<comments>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-two/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sun, 05 Mar 2017 09:21:30 +0000</pubDate>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JavaScript Basics]]></category>
		<category><![CDATA[JavaScript In Simple Language]]></category>
		<category><![CDATA[JavaScript Objects]]></category>
		<category><![CDATA[JavaScript Tutorial]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=12082</guid>

					<description><![CDATA[Here we are going to a see an anothe article of the JavaScript series. In the first part, we have see some basics that you can start with. In this article, we will be discussing about the JavaScript object continuations and constructors etc. You can always see my other posts related to JavaScript here. We will be using Visual Studio for our development. If you are totally new to JavaScript, I strongly recommend you to read some basics here.I hope you will like this. Now let&#8217;s begin. Download source code Do you know JavaScript &#8211; Part Two Background This article [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Here we are going to a see an anothe article of the <a href="http://sibeeshpassion.com/category/JavaScript" target="_blank">JavaScript</a> series. In the <a href="http://sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-1/" target="_blank">first part</a>, we have see some basics that you can start with. In this article, we will be discussing about the JavaScript object continuations and constructors etc.  You can always see my other posts related to JavaScript <a href="http://sibeeshpassion.com/category/JavaScript/" target="_blank">here</a>. We will be using <a href="http://sibeeshpassion.com/category/Visual-Studio/" target="_blank">Visual Studio</a> for our development. If you are totally new to JavaScript, I strongly recommend you to read some basics <a href="https://www.w3schools.com/js/" target="_blank">here</a>.I hope you will like this. Now let&#8217;s begin.</p>
<p><strong>Download source code</strong></p>
<li>
<a href="https://code.msdn.microsoft.com/Do-you-know-JavaScript-7506a9ec" target="_blank">Do you know JavaScript &#8211; Part Two</a>
</li>
<p><strong>Background</strong></p>
<p>This article is the second part of the series we have just started. If you haven&#8217;t read the first part yet, I stronglyh recommed you to read it <a href="http://sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-1/" target="_blank">here</a>. </p>
<p><strong>Setting up the platform</strong></p>
<p>To get started with, here we are going to create an HTML file and JS file. </p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Do you know JavaScript? Are you sure? Part Two&lt;/title&gt;<br />
	&lt;meta charset=&quot;utf-8&quot; /&gt;<br />
    &lt;script src=&quot;JavaScript.js&quot;&gt;&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><strong>Let&#8217;s begin our tutorial</strong></p>
<p>Now, please open your JavaScript file. We have some scripts to write.</p>
<p><strong>Constructor</strong></p>
<p>Have you ever created any constructor in JavaScript? Before going to create a one, it is beter to know what is the need for it. Let&#8217;s start with an example. Here I am going to create an object called bike. </p>
<p>[js]<br />
var bike = {<br />
    name: &quot;Thnuderbird&quot;,<br />
    color: &quot;Blue&quot;,<br />
    CC: &quot;350&quot;,<br />
    company: &quot;Royal Enfield&quot;,<br />
    tankCapacity: &quot;20&quot;,<br />
    abs: false,<br />
    showName: function () {<br />
        console.log(this.name);<br />
    }<br />
};<br />
[/js]</p>
<p>As you can read it, this object if holdong the common properties of my bike Royal Enfield Thunderbird. Now my question is this is the only bike you know? Absolutely no, right? So suppose you need to create an object for the brand new Bajaj Dominor? What will you do? Creating an another object? So what if you know more than 10 bikes. If your anser is creating 10 objects, that&#8217;s not fair at all. Instead, why don&#8217;t we share this common properties like name, color, cc ect&#8230;So we are going to create objects but we are not going to create the properties again. So let&#8217;s crete an object for Dominor. Before doing that, we need to create a bike construcor as follows.</p>
<p>[js]<br />
function Bike(name, color, cc, company, tankCapacity, abs) {<br />
    this.name = name;<br />
    this.color = color;<br />
    this.cc = cc;<br />
    this.company = company;<br />
    this.tankCapacity = tankCapacity;<br />
    this.abs = abs;<br />
    this.showName = function () {<br />
        console.log(&#8216;The name of the currnet bike is &#8216; + this.name);<br />
    }<br />
}<br />
[/js]</p>
<p>Now we can create object for Dominor as follows. Remember, when you write the word Bike, you can see that the intellisense is shown for you as follows.</p>
<div id="attachment_12083" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Constructor_intellisence-e1488699966952.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-12083" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Constructor_intellisence-e1488699966952.png" alt="Constructor_intellisence" width="634" height="305" class="size-full wp-image-12083" srcset="/wp-content/uploads/2017/03/Constructor_intellisence-e1488699966952.png 634w, /wp-content/uploads/2017/03/Constructor_intellisence-e1488699966952-300x144.png 300w, /wp-content/uploads/2017/03/Constructor_intellisence-e1488699966952-400x192.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12083" class="wp-caption-text">Constructor_intellisence</p></div>
<p>[js]<br />
var dominor = new Bike(&quot;Dominor&quot;, &quot;Black&quot;, &quot;Bajaj&quot;, &quot;20&quot;, true);<br />
dominor.showName();<br />
[/js]</p>
<p>Have you noticed that we have not created the function showName in the object dominor but in Bike, and we are still able to use that in the object dominor. And you can see an output as preceding in your browser console. </p>
<div id="attachment_12084" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Dominor_object_output.png"><img decoding="async" aria-describedby="caption-attachment-12084" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Dominor_object_output-e1488700077335.png" alt="Dominor_object_output" width="634" height="154" class="size-full wp-image-12084" srcset="/wp-content/uploads/2017/03/Dominor_object_output-e1488700077335.png 634w, /wp-content/uploads/2017/03/Dominor_object_output-e1488700077335-300x73.png 300w, /wp-content/uploads/2017/03/Dominor_object_output-e1488700077335-400x97.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12084" class="wp-caption-text">Dominor_object_output</p></div>
<p>And you can create an object for Royal Enfield Thunderbird as follows.</p>
<p>[js]<br />
var thunderbird = new Bike(&quot;Thunderbird&quot;, &quot;Marine&quot;, &quot;Royal Enfield&quot;, &quot;20&quot;, false);<br />
thunderbird.showName();<br />
[/js]</p>
<div id="attachment_12085" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Thunderbird_output-e1488700173116.png"><img decoding="async" aria-describedby="caption-attachment-12085" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Thunderbird_output-e1488700173116.png" alt="Thunderbird_output" width="634" height="171" class="size-full wp-image-12085" srcset="/wp-content/uploads/2017/03/Thunderbird_output-e1488700173116.png 634w, /wp-content/uploads/2017/03/Thunderbird_output-e1488700173116-300x81.png 300w, /wp-content/uploads/2017/03/Thunderbird_output-e1488700173116-400x108.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12085" class="wp-caption-text">Thunderbird_output</p></div>
<p><strong>Distinguish between own and inherited properties </strong></p>
<p>As the heading implies, we have two kind of properties, own and inherited. Let&#8217;s create an example to understand that.</p>
<p>[js]<br />
var thunderbird = new Bike(&quot;Thunderbird&quot;, &quot;Marine&quot;, &quot;Royal Enfield&quot;, &quot;20&quot;, false);<br />
thunderbird.isMine = true;</p>
<p>console.log(dominor.name + &quot; is yours or not? &quot;);<br />
console.log(&quot;isMine&quot; in dominor);<br />
console.log(thunderbird.name + &quot; is yours or not? &quot;);<br />
console.log(&quot;isMine&quot; in thunderbird);<br />
console.log(&quot;toString&quot; in dominor);<br />
console.log(&quot;toString&quot; in thunderbird);<br />
[/js]</p>
<p>Before runningit, Can you please guess what will be the output of the above coce block? If you find the answer, check whether your answer matches the below output.</p>
<div id="attachment_12087" style="width: 480px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Own_and_inherited_properties.png"><img decoding="async" aria-describedby="caption-attachment-12087" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Own_and_inherited_properties.png" alt="Own_and_inherited_properties" width="470" height="170" class="size-full wp-image-12087" srcset="/wp-content/uploads/2017/03/Own_and_inherited_properties.png 470w, /wp-content/uploads/2017/03/Own_and_inherited_properties-300x109.png 300w, /wp-content/uploads/2017/03/Own_and_inherited_properties-400x145.png 400w" sizes="(max-width: 470px) 100vw, 470px" /></a><p id="caption-attachment-12087" class="wp-caption-text">Own_and_inherited_properties</p></div>
<p>So isMine is the property that we jsut added to the object thunderbird, and the same is not available in the object dominor. That&#8217;s cool. But why the property toString is available in both object, we have not added that to our object right? This is because toString method is being inherited from the Object.prototype.</p>
<p><strong>Use of hasOwnProperty</strong></p>
<p>In the above code, we have just seen how to check any property is available in our object, but that doesn&#8217;t mean it is its own property right? To check that, we can use the hasOwnProperty. Let&#8217;s find out how to use it now.</p>
<p>[js]<br />
console.log(dominor.name + &quot; is yours or not? &quot;);<br />
console.log(dominor.hasOwnProperty(&quot;isMine&quot;));<br />
console.log(thunderbird.name + &quot; is yours or not? &quot;);<br />
console.log(thunderbird.hasOwnProperty(&quot;isMine&quot;));<br />
console.log(dominor.hasOwnProperty(&quot;toString&quot;));<br />
console.log(thunderbird.hasOwnProperty(&quot;toString&quot;));<br />
[/js]</p>
<p>Once again, please try to answer it your own, before you run it. </p>
<div id="attachment_12089" style="width: 367px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/hasOwnProperty_output.png"><img decoding="async" aria-describedby="caption-attachment-12089" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/hasOwnProperty_output.png" alt="hasOwnProperty_output" width="357" height="171" class="size-full wp-image-12089" srcset="/wp-content/uploads/2017/03/hasOwnProperty_output.png 357w, /wp-content/uploads/2017/03/hasOwnProperty_output-300x144.png 300w" sizes="(max-width: 357px) 100vw, 357px" /></a><p id="caption-attachment-12089" class="wp-caption-text">hasOwnProperty_output</p></div>
<p><strong>Looping through the object</strong></p>
<p>To loopthrough the each items in an object, you can use for loop as follow.</p>
<p>[js]<br />
for (var itm in thunderbird) {<br />
    console.log(itm);<br />
}<br />
[/js]</p>
<p>This is not the preffered way as we have not checked for hasOwnProperties, we are suppose to iterate only the properties which is its own. </p>
<div id="attachment_12090" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Looping_through_the_items_in_an_Object-e1488703343380.png"><img decoding="async" aria-describedby="caption-attachment-12090" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Looping_through_the_items_in_an_Object-e1488703343380.png" alt="Looping_through_the_items_in_an_Object" width="634" height="85" class="size-full wp-image-12090" srcset="/wp-content/uploads/2017/03/Looping_through_the_items_in_an_Object-e1488703343380.png 634w, /wp-content/uploads/2017/03/Looping_through_the_items_in_an_Object-e1488703343380-300x40.png 300w, /wp-content/uploads/2017/03/Looping_through_the_items_in_an_Object-e1488703343380-400x54.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12090" class="wp-caption-text">Looping_through_the_items_in_an_Object</p></div>
<p>So let&#8217;s rewrite the above code as follows.</p>
<p>[js]<br />
for (var itm in thunderbird) {<br />
    if (thunderbird.hasOwnProperty(itm)) {<br />
        console.log(itm + &quot;:&quot; + thunderbird[itm]);<br />
    }<br />
}<br />
[/js]</p>
<p>Here is the output.</p>
<div id="attachment_12091" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Iteration_with_hasOwnProperty-e1488703903787.png"><img decoding="async" aria-describedby="caption-attachment-12091" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Iteration_with_hasOwnProperty-e1488703903787.png" alt="Iteration_with_hasOwnProperty" width="634" height="227" class="size-full wp-image-12091" srcset="/wp-content/uploads/2017/03/Iteration_with_hasOwnProperty-e1488703903787.png 634w, /wp-content/uploads/2017/03/Iteration_with_hasOwnProperty-e1488703903787-300x107.png 300w, /wp-content/uploads/2017/03/Iteration_with_hasOwnProperty-e1488703903787-400x143.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12091" class="wp-caption-text">Iteration_with_hasOwnProperty</p></div>
<p>We have seen enough about objects right? Any idea how you can delete the property from an object?</p>
<p>[js]<br />
delete thunderbird.isMine;<br />
for (var itm in thunderbird) {<br />
    if (thunderbird.hasOwnProperty(itm)) {<br />
        console.log(itm + &quot;:&quot; + thunderbird[itm]);<br />
    }<br />
}<br />
[/js]</p>
<div id="attachment_12092" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/03/Delete_from_an_object-e1488704140711.png"><img decoding="async" aria-describedby="caption-attachment-12092" src="http://sibeeshpassion.com/wp-content/uploads/2017/03/Delete_from_an_object-e1488704140711.png" alt="Delete_from_an_object" width="634" height="216" class="size-full wp-image-12092" srcset="/wp-content/uploads/2017/03/Delete_from_an_object-e1488704140711.png 634w, /wp-content/uploads/2017/03/Delete_from_an_object-e1488704140711-300x102.png 300w, /wp-content/uploads/2017/03/Delete_from_an_object-e1488704140711-400x136.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12092" class="wp-caption-text">Delete_from_an_object</p></div>
<p>Now it is time for a question. What would be the output of the preceding code? </p>
<p>[js]<br />
console.log(delete thunderbird.toString);<br />
[/js]</p>
<p>It will return true, now run it again. What is the output? Again true? This is because of, toString is an inherited property so it won&#8217;t get deleted. So <em>thunderbird.toString</em> will always give you output.</p>
<p>That&#8217;s all for today. You can always download the source code attached to see the complete code and application. Happy coding!.</p>
<p><strong>References</strong></p>
<li><a href="https://www.w3schools.com/js/" target="_blank">JS</a></li>
<p><strong>See also</strong></p>
<li><a href="http://sibeeshpassion.com/category/JavaScript" target="_blank">Articles related to JavaScript</a></li>
<p><strong>Conclusion</strong></p>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<p><strong>Your turn. What do you think?</strong></p>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-two/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Do you know JavaScript? Are you sure? &#8211; Part 1</title>
		<link>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-1/</link>
					<comments>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-1/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Tue, 28 Feb 2017 17:22:18 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JavaScript Basics]]></category>
		<category><![CDATA[JavaScript In Simple Language]]></category>
		<category><![CDATA[JavaScript Objects]]></category>
		<category><![CDATA[JavaScript Simple Demo]]></category>
		<category><![CDATA[JavaScript Tutorial]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=12064</guid>

					<description><![CDATA[[toc] Introduction Here we are going to a series of articles related to JavaScript. As this is the first part of the series, here we are going to see some basics of JavaScript which you may forgot or you may not be aware of it. You can always see my other posts related to JavaScript here. We will be using Visual Studio for our development. Basically, JavaScript is a programming language of HTML and Web. Now a days we can create any kind of applications using JavaScript. If you are totally new to JavaScript, I strongly recommend you to read [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>Here we are going to a series of articles related to <a href="http://sibeeshpassion.com/category/JavaScript" target="_blank" rel="noopener">JavaScript</a>. As this is the first part of the series, here we are going to see some basics of JavaScript which you may forgot or you may not be aware of it. You can always see my other posts related to JavaScript <a href="http://sibeeshpassion.com/category/JavaScript/" target="_blank" rel="noopener">here</a>. We will be using <a href="http://sibeeshpassion.com/category/Visual-Studio/" target="_blank" rel="noopener">Visual Studio</a> for our development. Basically, JavaScript is a programming language of <a href="http://sibeeshpassion.com/category/html5/" target="_blank" rel="noopener">HTML</a> and Web. Now a days we can create any kind of applications using JavaScript. If you are totally new to JavaScript, I strongly recommend you to read some basics <a href="https://www.w3schools.com/js/" target="_blank" rel="noopener">here</a>. I hope you will like this. Now let&#8217;s begin.</p>
<h3><strong>Download source code</strong></h3>
<p>You can always download the source code from here <a href="https://code.msdn.microsoft.com/Do-you-know-JavaScript-ff7f3f98" target="_blank" rel="noopener">Do you know JavaScript &#8211; Part 1</a></p>
<h3><strong>Background</strong></h3>
<p>Recently I was assigned to a training task where I need to train one of my colleague who is a fresher and totally new to programming field. I had covered almost all the topics in my training and I am sharing few of them here. I hope you will like this.</p>
<h3>Using the code</h3>
<h4><strong>Create a HTML page</strong></h4>
<p>As we already said, JavaScript is a programming language of HTML and Web, we need a page to work with JavaScript. Let&#8217;s create it first.</p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;&lt;/title&gt;<br />
&lt;meta charset=&#8221;utf-8&#8243; /&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>Now create a JS file and include it in your page.</p>
<p>[js]<br />
&lt;script src=&#8221;JavaScript.js&#8221;&gt;&lt;/script&gt;<br />
[/js]</p>
<h4><strong>Let&#8217;s begin our tutorial</strong></h4>
<p>Any idea, what will be the output of the preceding lines of code.</p>
<p>[js]<br />
console.log(2 + 2);<br />
console.log(2 + &#8220;2&#8221;);<br />
console.log(2 + &#8220;2&#8221; + 2);<br />
console.log(2 + 2 + &#8220;2&#8221; + 2);<br />
console.log(2 + 2 + &#8220;2&#8221; + 2 + 2);<br />
console.log(2 + &#8220;2&#8221; + 2 + &#8220;2&#8221; + 2 + 2);<br />
console.log(&#8220;2&#8221; + 2 + &#8220;2&#8221; + 2 + 2 + 2 + 2);<br />
[/js]</p>
<p>Once after you run your application, press CTRL+SHIFT+J if you are opening your application in Google chrome or CTRL+SHIFT+K if it is Mozilla, or press F12 if in IE. This will open the browser console. Now check the output, is it the same as you thought?</p>
<div id="attachment_12065" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/02/Javascript_tutorial_output_1-e1488299369883.png"><img decoding="async" aria-describedby="caption-attachment-12065" class="size-full wp-image-12065" src="http://sibeeshpassion.com/wp-content/uploads/2017/02/Javascript_tutorial_output_1-e1488299369883.png" alt="Javascript_tutorial_output_1" width="634" height="197" srcset="/wp-content/uploads/2017/02/Javascript_tutorial_output_1-e1488299369883.png 634w, /wp-content/uploads/2017/02/Javascript_tutorial_output_1-e1488299369883-300x93.png 300w, /wp-content/uploads/2017/02/Javascript_tutorial_output_1-e1488299369883-400x124.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12065" class="wp-caption-text">Javascript_tutorial_output_1</p></div>
<p>If you noticed well, <em>console.log(2 + 2 + &#8220;2&#8221; + 2 + 2);</em> returned <em>4222</em>. First twos are added and when it comes to the third digit, the type of the result is being changed to string, so the string concatenation is happening. This is because of the dynamica nature of JavaScript.</p>
<p>So you can always add different types of values to the same variable in JavaScript. The preceding codes will always be working perfect.</p>
<p>[js]<br />
var a = 1;<br />
a = &#8220;Sibeesh&#8221;;<br />
[/js]</p>
<p>Now a simple test, what will be the output of the preceding code?</p>
<p>[js]<br />
var b;<br />
console.log(b);<br />
[/js]</p>
<p>Yes you are right it will return undefined as we have not assigned any values to the variable b. Have you ever checked the typeof an undefined variable?</p>
<p>[js]<br />
console.log(typeof(b));<br />
[/js]</p>
<p>The result will be undefined. Now what about the preceding codes?</p>
<p>[js]<br />
console.log(typeof(undefined));<br />
console.log(typeof(typeof(b)));<br />
console.log(typeof(String(b)));<br />
console.log(typeof({a:b}));<br />
console.log(typeof(null));<br />
[/js]</p>
<p>It can give you an output as below.</p>
<p>[js]<br />
undefined<br />
string<br />
string<br />
object<br />
object<br />
[/js]</p>
<p>Here one thing to be noted is, <em>typeof(typeof())</em> will always return string. And <em>{a:b}, null</em> is actually objects so the typeof will return the type as object. Let&#8217;s write a simple program now.</p>
<p>[js]<br />
var a=1;<br />
var b=a;<br />
a=2;</p>
<p>console.log(b);<br />
console.log(a);<br />
[/js]</p>
<p>See, how simple it was. Any idea what would be the output of that? If your answer is 1 &amp; 2, then you are right? Have you ever thought why is that? This is why the variable we just created are primitive types. In primitive data types the values are saved directly on the variable. If you are coming from the backgound of C#, you can understand the value typed variable.</p>
<p>Now we have one more data type which is referenced variable where values are stored as a reference not a direct values. An example for a referenced type variable is Object. Let&#8217;s go on to the next topic which is overview on JavaScript Objects, Do you know how to create an object?</p>
<p>[js]<br />
var myName = {firstName: &#8220;Sibeesh&#8221;, lastName: &#8220;Venu&#8221;};<br />
console.log(myName);<br />
console.log(JSON.stringify(myName));<br />
console.log(myName.firstName);<br />
console.log(myName.lastName);<br />
[/js]</p>
<p>Here myName is an object. You can always use JSON.stringify() to make your object to a string format and JSON.parse() to make your string to object. So the above code will give you an output as follows.</p>
<div id="attachment_12066" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/02/Javascript_tutorial_output_2-e1488300962889.png"><img decoding="async" aria-describedby="caption-attachment-12066" class="size-full wp-image-12066" src="http://sibeeshpassion.com/wp-content/uploads/2017/02/Javascript_tutorial_output_2-e1488300962889.png" alt="Javascript_tutorial_output_2" width="634" height="101" srcset="/wp-content/uploads/2017/02/Javascript_tutorial_output_2-e1488300962889.png 634w, /wp-content/uploads/2017/02/Javascript_tutorial_output_2-e1488300962889-300x48.png 300w, /wp-content/uploads/2017/02/Javascript_tutorial_output_2-e1488300962889-400x64.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12066" class="wp-caption-text">Javascript_tutorial_output_2</p></div>
<p>An object is a collection of key-value pairs, like we see in the above object, firstName is key and &#8220;Sibeesh&#8221; is value. How can we take any values from an object? We can always access a property from an object using dot(.) operator as follows.</p>
<p>[js]<br />
console.log(myName.firstName);<br />
console.log(myName.lastName);<br />
[/js]</p>
<p>And also using a bracket ([)</p>
<p>[js]<br />
console.log(myName[&#8220;firstName&#8221;]);<br />
console.log(myName[&#8220;lastName&#8221;]);<br />
[/js]</p>
<p>The above codes will give you an output as &#8220;Sibeesh&#8221; &#8220;Venu&#8221;, right? We just accesses a property from an object. Now we have given string value to our object as a key? Is there anyway we can give a key in number format? Let&#8217;s have a look.</p>
<p>[js]<br />
var myName = {1: &#8220;Sibeesh&#8221;, 2: &#8220;Venu&#8221;};<br />
[/js]</p>
<p>The question is, how can we access values of the keys 1 &amp; 2? Like below?</p>
<p>[js]<br />
console.log(myName.1);<br />
console.log(myName.2);<br />
[/js]</p>
<p>If you said &#8220;Yes&#8221;, you are wrong. If you try to accecc the values as above, This will throw an &#8220;Uncaught SyntaxError&#8221; error, so how can we access it?</p>
<p>[js]<br />
console.log(myName[&#8220;1&#8221;]);<br />
console.log(myName[&#8220;2&#8221;]);<br />
[/js]</p>
<p>So when the key value is a number, we must use [] for accessing the values. Now Lets go back to the previous object.</p>
<p>[js]<br />
var myName = {firstName: &#8220;Sibeesh&#8221;, lastName: &#8220;Venu&#8221;};<br />
var myNameCopy= myName;<br />
myName.firstName = &#8220;Sibi&#8221;;</p>
<p>console.log(myName.firstName);<br />
console.log(myNameCopy.firstName);<br />
[/js]</p>
<p>Any idea what will be the output of the above code? Have you just said Sibi Sibeesh? Oh, man. You got confused. The actual output is Sibi Sibi. As we said earlier, an object is an example of referenced type variable where the values are stored as a reference to it. So even if change the value of our first object, that will reflect is our second one too.</p>
<p>In the above examples we have created objects like below</p>
<p>[js]<br />
var myName = {firstName: &#8220;Sibeesh&#8221;, lastName: &#8220;Venu&#8221;};<br />
[/js]</p>
<p>This way of careating an object is called <em>Object literals</em>. Now the questuion is, is this the only way of creating the object? There is one more way, which is known as <em>Object Constructor</em>. Now we can create the same objects in Object constructor way.</p>
<p>[js]<br />
var myName = new Object();<br />
myName.firstName = &#8220;Sibeesh&#8221;<br />
myName.lastName = &#8220;Venu&#8221;;<br />
console.log(myName.firstName);<br />
console.log(myName.lastName);<br />
[/js]</p>
<p>Is there any rule that we can add only varibles to an object? Absolutely no, an object can hold a function too. Let&#8217;s create an object with a function inside.</p>
<p>[js]<br />
var myName = {<br />
firstName: &#8220;Sibeesh&#8221;,<br />
lastName: &#8220;Venu&#8221;,<br />
myFullName: function(){<br />
console.log(myName.firstName+&#8221; &#8220;+myName.lastName);<br />
}<br />
};<br />
[/js]</p>
<p>So the code <em>myName.myFullName();</em> will return my full name &#8220;Sibeesh Venu&#8221; as output. Right? So from the above code, we can create a functions in JavaScript as follows?</p>
<p>[js]<br />
var myFullName = function(firstName, lastName){<br />
return firstName + &#8221; &#8221; + lastName;<br />
}<br />
[/js]</p>
<p>If you call the above fucntion as preceding, you will get an output as &#8220;Sibeesh Venu&#8221;</p>
<p>[js]<br />
console.log(myFullName(&#8220;Sibeesh&#8221;,&#8221;Venu&#8221;));<br />
[/js]</p>
<p>The question is, what if we are passing only one values? Let&#8217;s try that out.</p>
<p>[js]<br />
console.log(myFullName(&#8220;Sibeesh&#8221;));<br />
[/js]</p>
<p>If you are working in any server side languages, this will actually give an error like &#8220;fucntion with one parameter couldn&#8217;t find&#8221;. But JavaScript is not like that, even if you are wrong, it will try to make you are right. So in this case, it actually treat the second parameter as undefined and give you an output as &#8220;Sibeesh undefined&#8221;.</p>
<p>That&#8217;s all for today. You can always download the source code attached to see the complete code and application. Happy coding!.</p>
<h3><strong>References</strong></h3>
<ul>
<li><a href="https://www.w3schools.com/js/" target="_blank" rel="noopener">JS</a></li>
</ul>
<h3><strong>See also</strong></h3>
<ul>
<li><a href="http://sibeeshpassion.com/category/JavaScript" target="_blank" rel="noopener">Articles related to JavaScript</a></li>
</ul>
<h3><strong>Conclusion</strong></h3>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h3><strong>Your turn. What do you think?</strong></h3>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/do-you-know-javascript-are-you-sure-part-1/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
