├── .DS_Store └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andyshora/front-end-interview-questions/HEAD/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Interview Questions for Front-end Developers 2 | ============================= 3 | 4 | How do you validate your markup and why? 5 | -------- 6 | W3C validator. 7 | 100% valid code is not always the goal, but it helps to write maintainable code. 8 | To ensure the code is rendered consistently cross-browser. 9 | 10 | Why use a Doctype? 11 | -------- 12 | A proper Doctype triggers standards mode in your browser. 13 | 14 | What is CDATA? 15 | -------- 16 | Used to be required to escape JavaScript in Doctypes prior to HTML5 in order to validate code. 17 | 18 | 19 | How do you waste time in JavaScript? 20 | -------- 21 | Redraw parts of the DOM, e.g. increase font-size in a loop. 22 | See http://www.bennadel.com/blog/2370-Overcoming-Asynchronous-Anxiety-By-Testing-JavaScript-s-Event-Loop.htm 23 | 24 | Explain hoisting in JavaScript 25 | -------- 26 | 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. 27 | 28 | 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. 29 | 30 | ```javascript 31 | var myvar = 'my value'; 32 | (function() { 33 | alert(myvar); // undefined 34 | var myvar = 'local value'; // only the declararion of the variable (var myvar) was hoisted to the top of the scoope 35 | })(); 36 | ``` 37 | Above, the declaration was hoisted, but the initialization ( = 'local value') was not, resulting in an undefined error. 38 | 39 | 40 | What is the difference between var x = 1 and x = 1? 41 | -------- 42 | Novice JS programmers might have a basic answer about locals vs globals 43 | 44 | Intermediate JS guys should definitely have that answer, and should probably mention function-level scope 45 | 46 | "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. 47 | 48 | See 49 | http://sharkysoft.com/tutorials/jsa/content/031.html 50 | http://schalk-neethling.com/2011/07/quick-tip-the-problem-with-implied-globals-in-javascript/ 51 | http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting 52 | http://stackoverflow.com/questions/1484143/scope-chain-in-javascript 53 | 54 | Are objects passed by reference or by value? 55 | ----------- 56 | 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. 57 | 58 | Is an Array an Object in JavaScript? 59 | ----------- 60 | Yes, Arrays inherit from Objects, and they have array-specific methods such as sort and length. 61 | 62 | Explain bit shifting in JavaScript 63 | ---------- 64 | Operands are treated as 32-bit integers. 65 | http://jsfiddle.net/andyshora/M4xAB/ 66 | ```javascript 67 | var temp; 68 | temp = 5 >> 1; // 101 -> 10 = 2 69 | temp = 8 >> 1; // 1000 -> 100 = 4 70 | temp = 10 >> 1; // 1010 -> 101 = 5 71 | // test 72 | document.write(temp); 73 | ``` 74 | 75 | What is the difference between using dot notation and bracket notation when accessing an object’s property? 76 | ----------- 77 | ```javascript 78 | val = ford.speed; // speed has to be a defined property 79 | 80 | val = ford['speed']; 81 | val = ford[ attr ]; // attr is a variable which may for example hold a string for the property name 82 | val = ford[ nextAttr() ]; // method call will return before accessing ford object 83 | ``` 84 | 85 | Why might a programmer have defined a variable with a capital letter? 86 | ----------- 87 | As a constructor for an Object. Instance names should be lower case. 88 | ```javascript 89 | // example 90 | 91 | var Car = new function(){ 92 | this.wheels = 4; 93 | } 94 | // instance of Car 95 | var ford = new Car(); 96 | ``` 97 | 98 | this and its scope 99 | ----------- 100 | ```javascript 101 | drink = 'soda'; //global variable 102 | 103 | var Foo = function(){ 104 | this.drink = 'beer'; //property of instance object 105 | console.log('Foo() -> ' + this.drink); 106 | }; 107 | 108 | function bar(){ 109 | console.log('bar() -> ' + this.drink); 110 | }; 111 | 112 | var qux = { 113 | drink: 'wine', // in the scope of qux, 'drink' = 'wine' 114 | getDrink: function(){ 115 | console.log('qux() -> ' + this.drink); 116 | } 117 | }; 118 | 119 | //now see how "this' differs in each case 120 | 121 | var baz = new Foo(); // Foo() -> beer 122 | bar(); // bar() -> soda 123 | qux.getDrink(); // qux() -> wine 124 | ``` 125 | 126 | 127 | What is a closure? 128 | -------- 129 | 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. 130 | 131 | What language is JavaScript based on? 132 | -------- 133 | ECMAScript = core language that JS is based on. 134 | 135 | 136 | What's the difference between a variable and a property? 137 | -------- 138 | var a = 'hello'; // variable 139 | You cant access variables with a . from the owner, you just need to know that they're there. 140 | 141 | Properties are the building blocks of objects. 142 | foo.bar 143 | 144 | They only appear interchangeable if the parent objects are the same 145 | 146 | 147 | Write a function to add arguments together 148 | --------- 149 | ```javascript 150 | function sum() { 151 | var i, l, result = 0; 152 | for (i = 0, l = arguments.length; i < l; i++) { 153 | result += parseInt(arguments[i]); 154 | } 155 | return result; 156 | } 157 | 158 | sum(1,2,3); // 6 159 | ``` 160 | And they should invoke it on your array like this (context for apply can be whatever, I usually use null in that case): 161 | ```javascript 162 | var data = [1,2,3]; 163 | sum.apply(null, data); // 6 164 | ``` 165 | 166 | Why use frameworks? 167 | --------- 168 | 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. 169 | 170 | Why use prototype to define methods instead of static methods? 171 | --------- 172 | 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. 173 | 174 | ```javascript 175 | // constructor function 176 | function MyClass () { 177 | var privateVariable; // private member only available within the constructor fn 178 | 179 | this.privilegedMethod = function () { // it can access private members 180 | //.. 181 | }; 182 | } 183 | 184 | // A 'static method', it's just like a normal function 185 | // it has no relation with any 'MyClass' object instance 186 | MyClass.staticMethod = function () {}; 187 | 188 | MyClass.prototype.publicMethod = function () { 189 | // the 'this' keyword refers to the object instance 190 | // you can access only 'privileged' and 'public' members 191 | }; 192 | 193 | var myObj = new MyClass(); // new object instance 194 | 195 | myObj.publicMethod(); 196 | MyClass.staticMethod(); 197 | ``` 198 | 199 | Public, private and privaledged members in JavaScript 200 | --------- 201 | ```javascript 202 | //Private 203 | 204 | // Constructor 205 | function Kid (name) { 206 | // Private 207 | var idol = "Paris Hilton"; 208 | } 209 | ``` 210 | 211 | You can delete or replace a privileged method, but you cannot alter its contents. 212 | ```javascript 213 | // Constructor 214 | function Kid (name) { 215 | // Private 216 | var idol = "Paris Hilton"; 217 | 218 | // Privileged 219 | this.getIdol = function () { 220 | return idol; 221 | }; 222 | } 223 | ``` 224 | 225 | 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 226 | ```javascript 227 | // Constructor 228 | function Kid (name) { 229 | // Constructor code 230 | } 231 | 232 | // Static property 233 | Kid.town = "South Park"; 234 | 235 | 236 | // Public 237 | // Constructor 238 | function Kid (name) { 239 | // Public 240 | this.name = name; 241 | } 242 | Kid.prototype.getName = function () { 243 | return this.name; 244 | }; 245 | 246 | // ---------------------- all examples 247 | 248 | // Constructor 249 | function Kid (name) { 250 | // Private 251 | var idol = "Paris Hilton"; 252 | 253 | // Privileged 254 | this.getIdol = function () { 255 | return idol; 256 | }; 257 | 258 | // Public 259 | this.name = name; 260 | } 261 | 262 | // Public 263 | Kid.prototype.getName = function () { 264 | return this.name; 265 | }; 266 | 267 | // Static property 268 | Kid.town = "South Park"; 269 | 270 | // ---------------- usage 271 | 272 | // Create a new instance 273 | 274 | var cartman = new Kid("Cartman"); 275 | 276 | // Access private property 277 | cartman.idol; // undefined 278 | 279 | // Access privileged method 280 | cartman.getIdol(); // "Paris Hilton" 281 | 282 | // Access public property 283 | cartman.name; // "Cartman" 284 | 285 | // Access public method 286 | cartman.getName(); // "Cartman" 287 | 288 | // Access static property on an instance 289 | cartman.town; // undefined 290 | 291 | // Access static property on the constructor object 292 | Kid.town; // "South Park" 293 | ``` 294 | 295 | See http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/ 296 | 297 | What is Progressive Enhancement? 298 | ---------- 299 | Progressive Enhancement consists of the following core principles: 300 | 301 | basic content should be accessible to all browsers 302 | basic functionality should be accessible to all browsers 303 | sparse, semantic markup contains all content 304 | enhanced layout is provided by externally linked CSS 305 | enhanced behavior is provided by [[Unobtrusive JavaScript|unobtrusive]], externally linked JavaScript 306 | end user browser preferences are respected 307 | 308 | Can you describe the difference between progressive enhancement and graceful degradation? 309 | ---------- 310 | Describe feature detection. 311 | When features are not supported, maintain accessibility. 312 | 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. 313 | 314 | If you have 10 different stylesheets for a given design, how would you integrate them into the site? 315 | ---------- 316 | File concatenation and minification in the build process. 317 | Don't use @import as it results in an additional request. 318 | 319 | How would you reduce page load time/perceived load time? 320 | ---------- 321 | Reduce image sizes 322 | Use image sprites 323 | Image datauris inline for small things like avatars 324 | Concatenate assets 325 | Host assets on different hostnames 326 | Use CDN for assets 327 | Load content async and feedback loading state to user 328 | Minify scripts and stylesheets 329 | Page data 330 | Cache files 331 | 332 | 333 | What is FOUC? How do you avoid FOUC? 334 | ---------- 335 | http://www.learningjquery.com/2008/10/1-way-to-avoid-the-flash-of-unstyled-content 336 | http://paulirish.com/2009/avoiding-the-fouc-v3/ 337 | 338 | 339 | What's a doctype do, and how many can you name? 340 | ---------- 341 | 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. 342 | 343 | 344 | To define the language of a section of the document, add the lang attribute to the appropriate element, such as a div element: 345 | 346 | 347 | How do you serve a page with content in multiple languages? 348 | ---------- 349 | ```html 350 |