Skip to content

Instantly share code, notes, and snippets.

@mswanson
Last active June 9, 2016 22:33
Show Gist options
  • Save mswanson/495f82bd3e9abac3b3e4cc660cbb40cb to your computer and use it in GitHub Desktop.
Save mswanson/495f82bd3e9abac3b3e4cc660cbb40cb to your computer and use it in GitHub Desktop.

Frontend Interview Questions

HTML

What is the difference between a <span> and a <div>?

Answer:

  • <div> is a block level element which means it will render it on it's own line with a width of a 100% of the parent element.
  • <span> is an inline element which means it will render on the same line as the previous element, if it is also an inline element, and it's width will be determined by it's content.

Name 5 common block-level and inline HTML elements.

Answer:

  • block elements: <h1>, <p>, <ul>, <ol>, <li>,
  • inline elements: <span>, <a>, <strong>, <i>, <img>

Define semantic markup. What are the semantic meanings for <section>, <article>, <aside>, <nav>, <header>, <footer> and when/how should each be used in structuring html markup?

Answer:

  • <section> defines a section in a document or a block of related elements.
  • <article> specifies independent, self-contained content.
  • <aside> defines some content aside from the content it is placed in.
  • <nav> defines a set of navigation links and intended for large blocks of navigation links; not all links in a document should be inside a <nav> element.
  • <header specifies a header for a document or section and should be used as a container for introductory content. There can be several <header> elements in one document.
  • <footer> specifies a footer for a document or section and should contain information about its containing element. There can be several <footer> elements in one document.

What is the difference between (<i> and <em>) and (<b>, <strong>) and how will screen-readers react the to each tag?

Answer: <i> and <b> simply indicate style and have no semantic meaning. While <em> and <strong> indicate some semantic meaning about the content they wrap and screen-readers will read this content differently.


What is the purpose of aria attributes?

Answer: They communicate role, state and property semantics to assistive technologies via the accessibility APIs implemented in browsers


Explain what inline styles and javascript are and when they are appropriate to use.

Answer:


When is it acceptable to use font tags?

Answer: Never.


Javascript

Explain how prototypal inheritance works

Answer: Instances inherit directly from other objects without any implicit hierarchy. Instances are typically instantiated via factory functions using the new keyword or Object.create().


What is a closure in Javascript? Provide and example.

Answer: A closure is a function defined inside another function (called the parent function), and has access to variables that are declared and defined in the parent function scope. The closure has access to variables in three scopes:

  • Variables declared in their own scope
  • Variables declared in a parent function scope
  • Variables declared in the global namespace
  let someGlobalVar = 'foo'
  function parentFunction(someArrayVar = ['good','better','best']) {
    const someConstVar = 'bar';
    let someLetVar = 'baz';
    someArrayVar.forEach((item) => {
      console.log(item);
      console.log(someGlobalVar);
      console.log(someConstVar);
      console.log(someLetVar);
    });
  }

What will the output of this function be? Explain why.

function test() {
   console.log(a);
   console.log(foo());
   var a = 1;
   function foo() {
      return 2;
   }
}

Answer: undefined 2. In JavaScript, variables and functions are hoisted. Basically, the Javascript interpreter looks ahead to find all variable and function declarations and then hoists them to the top of the function in which they're declared


Explain the difference between var, let and const and when each should be used.

Answer: const and let are block scoped where as var is either function or globally scoped.

  • const signals that the variable won’t be reassigned.
  • let, signals that the variable may be reassigned.
  • var gives no signal about the variable and should be avoided.

Give an example of 4 truthy and 4 falsey values.

Answer:

  • truthy: 1, 'abc', {}, []
  • falsey: 0, null, '', undefined

Why is extending built-in JavaScript objects not a good idea?

Answer:

  • Dependability - A consistent and dependable execution environment is crucial for maintainability.
  • Compatibility - Mmodifying objects that we don’t own opens the possibility of naming collisions and incompatible implementations

What is the difference between == and ===?

Answer: == will not check types and === will check whether both sides are of same type.

Bonus Points: Under the hood == tries to convert both sides to the same type and then do the comparison. This is called coercion. === compares the types and values if both sides are not same type, the answer is always false.


What is the difference between synchronous and asynchronous requests?

Answer: Synchronous requests block execution until response is retrieved whereas asynchronous does not.

Explain AJAX in as much detail as you can. Give an example.

Answer: Ajax (Asynchronous Javascript and XML) uses asynchronous requests to transfer json data between the browser and the backend server.

Bonus Points: for any mention of XMLHttpRequests.


Explain Javascript Promises in as much detail as you can. Give an example.

Answer: Promises allow you to write asynchronous code in a synchronous way. Using thenable functions to handle settled promises. A promise can be in one of 4 states:

  • fulfilled - the action succeeded
  • rejected - the action failed
  • pending - the action hasn't fulfilled or rejected yet
  • settled - the action has been fulfilled or rejected
  const promise = new Promise((resolve, reject) => {
    // do some async call
    if (/*async call succeeded*/) {
      resolve(/* some response */);
    } else {
      reject(/* some error */)
    }
  });

Explain how the this keyword functions in Javascript.

Answer: At the time of execution of every function, Javascript engine sets a property to the function called this which refer to the current execution context.

In the browser's global context what does this refer to?

Answer: window

In the context of an object function what does this refer to?

Answer: The functions containing object.

How do ES6 arrow functions handle this?

Answer: They pass the current context into the called function context.


In what order will the numbers 1-4 be logged to the console when the code below is executed? Why?

function test() {
  console.log(1);
  setTimeout(function(){console.log(2)}, 1000);
  setTimeout(function(){console.log(3)}, 0);
  console.log(4);
};

Answer: The values will be logged in the following order: 1,4,3,2.

  • 1 and 4 are displayed first since they are logged by simple calls to console.log() without any delay
  • 2 is displayed after 3 because 2 is being logged after a delay of 1000ms and 3 is being logged after a delay of 0 ms.
  • Since setTimeout() puts execution of its referenced function into the browser's event queue which will not run until the next event loop both 3 and 2 are displayed after 4.

Testing and Debugging

What tools and techniques do you use debugging JavaScript code?
What is the difference between a unit test and a functional/integration test?
What tools would you use to test your code's functionality?
What are the steps you take to debug a javascript error?

Other Javascript Questions

Have you ever used any Javascript templating libraries? If so, which ones?

CSS

What does the 'C' in CSS stands for?

Answer: Cascading

How does cascading determine precedence in assigning styles? Give a few examples.

Answer: Basically the order and specificity style rules affect precedence. For a given set of style rules for a specific selector, the further down the cascade a rule appears the higher it's precedence and the more specific a rule the higher the precedence.

#some-id { color: yellow; }
p.some-class { color: blue; }
.some-class { color: red; }
.some-class { color: green; }

Given this css the text will be yellow. If you removed the ID it would still be blue.

What is the difference between the way CSS handles classes and IDs?

Answer: IDs have higher precedence in the Cascade than Classes


What does !important do and when is it acceptable to use?

Answer: It overrides the cascade and gives the style rule the highest precedence.


What's the difference between "resetting" and "normalizing" CSS? Which would you choose, and why?

Answer: resetting will remove all the default styling applied by the browser to give you a blank canvas where as normalize is a base stylesheet meaning its the starting point for custom styles and it styles the default elements to be consistent across the browsers.


What's the difference between block, inline and inline-block displayed elements?

Answer:

  • Block elements:
    • respect all of those
    • force a line break after the block element
  • Inline elements:
    • respect left & right margins and padding, but not top & bottom
    • cannot have a width and height set
    • allow other elements to sit to their left and right.
  • Inline-block elements:
    • allow other elements to sit to their left and right
    • respect top & bottom margins and padding
    • respect height and width

What's the difference between a relative, fixed, absolute and statically positioned element?

Answer:

  • Static - default for every single page element. The only reason you would ever set an element to position: static is to forcefully-remove some positioning that got applied to an element outside of your control.
  • Relative - means "relative to itself". Setting position: relative; on an element and no other positioning attributes, it will no effect on it's positioning. It allows the use of z-index on the element and it limits the scope of absolutely positioned child elements. Any child element will be absolutely positioned within that block.
  • Absolute - positions the element exactly where you want it rrelative to the next parent element with relative (or absolute) positioning. If there is no such parent, it will default all the way back up to the <html> element.
  • Fixed - positioned relative to the viewport, or the browser window itself. regardless of scroll position.

Describe z-index and how stacking context is formed.

Answer: An element with greater stack order is always in front of an element with a lower stack order. z-index only works on positioned elements. The default stack order of non-positioned elements is their order in the document.


Explain your understanding of the box model and how you would tell the browser in CSS to render your layout in different box models.

Answer: The content of the space taken by an element. Each element has an inner and outer height/width calculated based on the content, padding, border and margin.

  • content-box - default. Excludes padding, border and margin from the inner dimensions.
  • border-box - includes the padding and border, but not the margin in the inner dimension.
What does * { box-sizing: border-box; } do? What are its advantages?

Answer: Make every element in the document include the padding and border in the element's inner dimensions; making it easier to reason about the layout of elements on the page.


Describe Floats and Flexbox and how each works. Why Flexbox is a better solution for web layouts?

Answer:

  • Floats - specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it.
  • Flexbox - consists of flex containers and flex items. Flex containers wrap a set of flex items and define how they are laid out. Flex items has properites that define how they interact with sibling flex elements and can also be flex containers themselves.

Other CSS Questions

What CSS frameworks have you used?
Which CSS Preprocessors have you used?

Sass Specific Questions

Explain nested selectors in Sass.

Answer:


How do you refer to a parent selector in the SASS?

Answer: & selects the parent selector in a nested selector context.


Explain what Sass Maps are and how they are use?

Answer:


Explain the @import function and how it is used.

Answer:


Explain the @include, @mixin, @function functions and how they are used.

Answer:


Explain how %placeholder, @extend are used in Sass and the potential pitfalls of using them.

Answer:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment