Repository: andyshora/front-end-interview-questions Branch: master Commit: 0e842614d81f Files: 1 Total size: 19.1 KB Directory structure: gitextract_pa5lu4fo/ └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ Interview Questions for Front-end Developers ============================= How do you validate your markup and why? -------- W3C validator. 100% valid code is not always the goal, but it helps to write maintainable code. To ensure the code is rendered consistently cross-browser. Why use a Doctype? -------- A proper Doctype triggers standards mode in your browser. What is CDATA? -------- Used to be required to escape JavaScript in Doctypes prior to HTML5 in order to validate code. How do you waste time in JavaScript? -------- Redraw parts of the DOM, e.g. increase font-size in a loop. See http://www.bennadel.com/blog/2370-Overcoming-Asynchronous-Anxiety-By-Testing-JavaScript-s-Event-Loop.htm Explain hoisting in JavaScript -------- In JavaScript function declarations ( function foo() {} ) and variable declarations ( var bar ) are ‘hoisted’ i.e. are silently moved to the very top of the scope. Within its current scope, regardless of where a variable is declared, it will be, behind the scenes, hoisted to the top. However, only the declaration will be hoisted. If the variable is also initialized, the current value, at the top of the scope, will initially be set to undefined. ```javascript var myvar = 'my value'; (function() { alert(myvar); // undefined var myvar = 'local value'; // only the declararion of the variable (var myvar) was hoisted to the top of the scoope })(); ``` Above, the declaration was hoisted, but the initialization ( = 'local value') was not, resulting in an undefined error. What is the difference between var x = 1 and x = 1? -------- Novice JS programmers might have a basic answer about locals vs globals Intermediate JS guys should definitely have that answer, and should probably mention function-level scope "advanced" JS programmer should be prepared to talk about locals, implied globals (defined without var, assumed belongs to window DOM, problem if third-party script uses same var name and uses var, they can conflict), the window object, function-scope, declaration hoisting (if statements do not create new scope, only functions do), and . Furthermore, I'd love to hear about [[DontDelete]], hoisting precedence (parameters vs var vs function), and undefined. See http://sharkysoft.com/tutorials/jsa/content/031.html http://schalk-neethling.com/2011/07/quick-tip-the-problem-with-implied-globals-in-javascript/ http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting http://stackoverflow.com/questions/1484143/scope-chain-in-javascript Are objects passed by reference or by value? ----------- By reference. In JavaScript, all objects are passed by reference. When you make a change to a reference to an object, you change the actual object. Primitive types are passed by value. Is an Array an Object in JavaScript? ----------- Yes, Arrays inherit from Objects, and they have array-specific methods such as sort and length. Explain bit shifting in JavaScript ---------- Operands are treated as 32-bit integers. http://jsfiddle.net/andyshora/M4xAB/ ```javascript var temp; temp = 5 >> 1; // 101 -> 10 = 2 temp = 8 >> 1; // 1000 -> 100 = 4 temp = 10 >> 1; // 1010 -> 101 = 5 // test document.write(temp); ``` What is the difference between using dot notation and bracket notation when accessing an object’s property? ----------- ```javascript val = ford.speed; // speed has to be a defined property val = ford['speed']; val = ford[ attr ]; // attr is a variable which may for example hold a string for the property name val = ford[ nextAttr() ]; // method call will return before accessing ford object ``` Why might a programmer have defined a variable with a capital letter? ----------- As a constructor for an Object. Instance names should be lower case. ```javascript // example var Car = new function(){ this.wheels = 4; } // instance of Car var ford = new Car(); ``` this and its scope ----------- ```javascript drink = 'soda'; //global variable var Foo = function(){ this.drink = 'beer'; //property of instance object console.log('Foo() -> ' + this.drink); }; function bar(){ console.log('bar() -> ' + this.drink); }; var qux = { drink: 'wine', // in the scope of qux, 'drink' = 'wine' getDrink: function(){ console.log('qux() -> ' + this.drink); } }; //now see how "this' differs in each case var baz = new Foo(); // Foo() -> beer bar(); // bar() -> soda qux.getDrink(); // qux() -> wine ``` What is a closure? -------- A closure is formed when you nest functions, inner functions can refer to the variables present in their outer enclosing functions even after their parent functions have already executed. What language is JavaScript based on? -------- ECMAScript = core language that JS is based on. What's the difference between a variable and a property? -------- var a = 'hello'; // variable You cant access variables with a . from the owner, you just need to know that they're there. Properties are the building blocks of objects. foo.bar They only appear interchangeable if the parent objects are the same Write a function to add arguments together --------- ```javascript function sum() { var i, l, result = 0; for (i = 0, l = arguments.length; i < l; i++) { result += parseInt(arguments[i]); } return result; } sum(1,2,3); // 6 ``` And they should invoke it on your array like this (context for apply can be whatever, I usually use null in that case): ```javascript var data = [1,2,3]; sum.apply(null, data); // 6 ``` Why use frameworks? --------- Good coders code, great coders reuse. Thousands of man hours have been poured into these libraries to abstract DOM capabilities away from browser specific implementations. There's no reason to go through all of the different browser DOM headaches yourself just to reinvent the fixes. Why use prototype to define methods instead of static methods? --------- You are extending the constructor function when you use prototype, so it will be available to all the object instances created with the new keyword, and the context within that function (the this keyword) will refer to the actual object instance where you call it. ```javascript // constructor function function MyClass () { var privateVariable; // private member only available within the constructor fn this.privilegedMethod = function () { // it can access private members //.. }; } // A 'static method', it's just like a normal function // it has no relation with any 'MyClass' object instance MyClass.staticMethod = function () {}; MyClass.prototype.publicMethod = function () { // the 'this' keyword refers to the object instance // you can access only 'privileged' and 'public' members }; var myObj = new MyClass(); // new object instance myObj.publicMethod(); MyClass.staticMethod(); ``` Public, private and privaledged members in JavaScript --------- ```javascript //Private // Constructor function Kid (name) { // Private var idol = "Paris Hilton"; } ``` You can delete or replace a privileged method, but you cannot alter its contents. ```javascript // Constructor function Kid (name) { // Private var idol = "Paris Hilton"; // Privileged this.getIdol = function () { return idol; }; } ``` A static member is shared by all instances of the class as well as the class itself (i.e. the Kid object), but it is only stored in one place. This means that its value is not inherited down to the object’s instances ```javascript // Constructor function Kid (name) { // Constructor code } // Static property Kid.town = "South Park"; // Public // Constructor function Kid (name) { // Public this.name = name; } Kid.prototype.getName = function () { return this.name; }; // ---------------------- all examples // Constructor function Kid (name) { // Private var idol = "Paris Hilton"; // Privileged this.getIdol = function () { return idol; }; // Public this.name = name; } // Public Kid.prototype.getName = function () { return this.name; }; // Static property Kid.town = "South Park"; // ---------------- usage // Create a new instance var cartman = new Kid("Cartman"); // Access private property cartman.idol; // undefined // Access privileged method cartman.getIdol(); // "Paris Hilton" // Access public property cartman.name; // "Cartman" // Access public method cartman.getName(); // "Cartman" // Access static property on an instance cartman.town; // undefined // Access static property on the constructor object Kid.town; // "South Park" ``` See http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/ What is Progressive Enhancement? ---------- Progressive Enhancement consists of the following core principles: basic content should be accessible to all browsers basic functionality should be accessible to all browsers sparse, semantic markup contains all content enhanced layout is provided by externally linked CSS enhanced behavior is provided by [[Unobtrusive JavaScript|unobtrusive]], externally linked JavaScript end user browser preferences are respected Can you describe the difference between progressive enhancement and graceful degradation? ---------- Describe feature detection. When features are not supported, maintain accessibility. Content should always be accessible, but user experience sacrificed for older browsers. Try to keep all functionality, and try to keep custom solutions compartmentalised where necessary, for example, in external files loaded for specific browsers. If you have 10 different stylesheets for a given design, how would you integrate them into the site? ---------- File concatenation and minification in the build process. Don't use @import as it results in an additional request. How would you reduce page load time/perceived load time? ---------- Reduce image sizes Use image sprites Image datauris inline for small things like avatars Concatenate assets Host assets on different hostnames Use CDN for assets Load content async and feedback loading state to user Minify scripts and stylesheets Page data Cache files What is FOUC? How do you avoid FOUC? ---------- http://www.learningjquery.com/2008/10/1-way-to-avoid-the-flash-of-unstyled-content http://paulirish.com/2009/avoiding-the-fouc-v3/ What's a doctype do, and how many can you name? ---------- When you use a DOCTYPE declarations in your web pages, you are telling the web browser what version of (X)HTML your web page should be displayed in. The doctype gives the browser a list of supported tags and does not include any deprecated or proprietary tags in the list. You can get away with writing invalid or incorrect HTML code because most web browsers are amazingly forgiving. To define the language of a section of the document, add the lang attribute to the appropriate element, such as a div element: How do you serve a page with content in multiple languages? ---------- ```html