JavaScript


What is an object ?

  1. JavaScript object is a standalone entity that holds multiple values in terms of properties and methods.
  2. Object property stores a literal value and method represents function.
  3. An object can be created using object literal or object constructor syntax.
  4. Object literal:var person = { firstName: “James”, lastName: “Bond”, age: 25, getFullName: function () { return this.firstName + ‘ ‘ + this.lastName } };
  5. Object constructor: var person = new Object(); person.firstName = “James”; person[“lastName”] = “Bond”; person.age = 25; person.getFullName = function () { return this.firstName + ‘ ‘ + this.lastName; };
  6. Object properties and methods can be accessed using dot notation or [ ] bracket.
  7. An object is passed by reference from one function to another.
  8. An object can include another object as a property.

Can I store JavaScript Functions in arrays?

var films = {
film1: “Jurassic Park”,
film2: “Titanic,
film3: “Toy Story”,
// …
};

var Film = {
// Initialise le film
init: function (title, year) {
this.title = title;
this.year = year;
},
// Renvoie la description du film
describe: function () {
var description = this.title + ” (” + this.year + “)”;
return description;
}
};

var film1 = Object.create(Film);
film1.init(“Jurassic Park”, 1993);

var film2 = Object.create(Film);
film2.init(“Titanic”, 1997);

var film3 = Object.create(Film);
film3.init(“Toy Story”, 1995);

// …

var films = [];
films.push(film1);
films.push(film2);
films.push(film3);

films.forEach(function (film) {
console.log(film.describe());
});

var Functions = { DoThis: function() { alert(“do this”); } };

Functions[‘DoThis’](); //alerts “do this”
Functions.DoThis()

What are JavaScript types?

Following are the JavaScript types:

  1. Number
  2. String
  3. Boolean
  4. Function
  5. Object
  6. Null
  7. Undefined

What is the use of isNaN function?

isNan function returns true if the argument is not a number otherwise it is false.

What is Negative Infinity?

Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.

What are undeclared and undefined variables?

Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.

Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.