Introduction
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with
JavaScript and what mistakes generally developers make and what differences they should be aware of.
Data types
A very common word and try basic in a programming language, JavaScript provides different data types to hold different types of values.
Q: How many data types are there in JS?
Ans: There are two types of data types in JavaScript.
Primitive data type
- String
- Number
- Boolean
- Undefined
- Null
Non-primitive (reference) data type
Q: How do you specify the data types in JS?
Ans: It’s simple and could be defined using var keyword only
Ex
- var x=10;
- var x=”javascript”;
As a developer, I am always curious to know the size of data types. But there is no sizeof function available which provides me size in JavaScript. But it’s good to know memory consumption when we work on bigger applications.
Download sizeof.js
Download one of the files below and upload it to your web server.
Example
-
- var object = {
- 'boolean': true,
- 'number': 1,
- 'string': 'a',
- 'array': [1, 2, 3]
- };
-
- var size = sizeof(object);
-
Q: What is the difference between Undefined and Null?
Ans: In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such a,
- var TestVar;
- alert(TestVar);
- alert(typeof TestVar);
- null is an assignment value.It can be assigned to a variable as no value: var TestVar = null;
- alert(TestVar);
- alert(typeof TestVar);
Objects
Objects are variables too. However, objects can contain many values.
Ex
- var person = {age:59, address:"Delhi”};
Q: Do you know a simple way to create objects?
Ans
- var obj = {};
-
- typeof(obj);
- "object"
- Object Properties
- You can access object properties in two ways: objectName.propertyName
- or
- objectName["propertyName"]
Ex
- person.age
- Or
- Person[“age”]
Object Methods
You access an object method with the following syntax:
objectName.methodName()
Example
- var person = {
- age: 59,
- address: "Delhi",
- fullName: function() {
- console.log('John Doe');
- return 'John Doe';
- }
- };
If you access the property without parenthesis (), it will return the function definition:
person.fullName
-
- Otherwise it’ ll execute the method
- person.fullName();
-
Loop over all properties in Object
We often use FOR statement to loop, but there is another feature, for in, which is pretty useful to iterate over all properties of Object
Ex
- for(a in person){console.log(a)}
- age
- address
- fullName
Q: How do you add/delete properties to existing object?
Ans: It is simple in Javascript, by giving a value
Ex
- person.country = “India”;
To delete you could use the delete keyword
Ex
Access operator
Goto browser console window and try to access all properties /methods via access operators, ex-
Q: Can you access a property, which does not exist in Object?
Ans: Yes, you can access a property on an object that does not exist but will result is undefined.
Ex
Q: Can you access a method, which does not exist in Object?
Ans: No, you cannot access a method on an object that does not exist because it’ll yield an error.
Ex
Read more articles on JavaScript
Hassan ChaabanPosted May 29, 2016, 9:01 AM
Verry clear,nice explanation, i love it
Guest UserPosted May 29, 2016, 2:29 AM
Hi Vinayak, can yuu please tell me which tool are you interested in?
Debasis SahaPosted May 23, 2016, 1:51 PM
Good One..
vinayak ghantiPosted May 22, 2016, 3:15 PM
super i loved it , can you suggest any tool for JS
Amit DavePosted May 3, 2016, 6:56 AM
Nice article!
Upendra Pratap ShahiPosted Apr 30, 2016, 4:01 PM
Thanks @sumit sir...
RakeshPosted Apr 29, 2016, 3:00 PM
Good one sir
Ravi PatelPosted Apr 25, 2016, 9:37 AM
thanks sir. for nice explanation
Vishvajeet PawarPosted Apr 25, 2016, 3:59 AM
good one
Guest UserPosted Apr 24, 2016, 11:03 PM
Thanks Mahesh Chand, I am glad you liked the title!
Mahesh ChandPosted Apr 24, 2016, 9:59 PM
Nice Sumit. I like title Voice of a Developer :)
Kumaresh RajalingamPosted Apr 24, 2016, 9:35 PM
Nice share
Rahul Kumar SaxenaPosted Apr 24, 2016, 12:20 PM
Good One
Vignesh ManiPosted Apr 24, 2016, 9:50 AM
Good one
Vignesh ManiPosted Apr 24, 2016, 9:50 AM
Nice one
Guest UserPosted Apr 24, 2016, 12:32 AM
Thanks guys!
Bhuvanesh MohankumarPosted Apr 23, 2016, 6:20 PM
Good
Kuppurasu NagarajPosted Apr 23, 2016, 1:31 PM
Nice Sharing..
Upendra Pratap ShahiPosted Apr 23, 2016, 9:35 AM
nice explain sir...