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 ry vebasic in 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 data type 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 datatypes. 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 in 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 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 delete keyword
Ex-
delete person.age;
Access operator
Goto browser console window and try to access all properties /methods via access operators, ex-
Q: Can you access a property, which do not exist in Object?
Ans: Yes, you can access a property on an object that does not exist but will result in undefined.
Ex-
Q: Can you access a method, which do 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: