5 questions you should know how to answer if you program in javascript - questions interviewers can ask you in a job interview


In this article you will see some javascript concepts that should be clear in your mind if you are doing a job interview.

1 - What are the javascript programming paradigms?

Paradigms are a way of classifying the writing of code based on certain factors. Languages ​​can be classified into various paradigms.

some paradigms are:

  • imperative that the programmer instructs the machine how to change its state,
  1. procedural that groups instructions into procedures,
  2. object-oriented, which groups instructions with the part of the state in which they operate,
  • declarative in which the programmer merely declares properties of the desired result, but not how to calculate it
  1. functional where the desired result is declared as the value of a series of function applications,
  2. logic in which the desired result is stated as the answer to a question about a system of facts and rules,
  3. mathematics in which the desired result is declared as the solution to an optimization problem
  4. reactive where the desired result is declared with data streams and change propagation

Your answer should be something like:

JavaScript is a multiparadigm language. It has the procedural, object-oriented and functional paradigm

Good to know:

  • Prototype-based programming is an object-oriented style of programming in which behavior reuse is accomplished through a process of reusing existing objects that serve as prototypes.
  • functional programming is a programming paradigm that treats computation as an evaluation of mathematical functions and that avoids mutable states or data. It emphasizes the application of functions, in contrast to imperative programming, which emphasizes changes in program state.

2 - What is Functional Programming?

Functional programming is one of the main concepts of javascript and one of its pillars.

Functional programming is a programming paradigm. In other words, it's a way of thinking about writing software based on specific principles that have been defined and listed below. Other examples of programming paradigms include object-oriented programming and procedural programming.

The main concepts:

  • Pure functions: Dadas as mesmas entradas, sempre retorna a mesma saída, e Não tem efeitos colaterais.

  • Function composition: is the process of combining two or more functions to produce a new function or perform some calculation. No side effects

  • Avoid shared state: shared state is any variable, object or memory space that can be changed in more than one place or by more than one function. The idea is to avoid states like this to avoid bugs. A function can be called in many different states and can downgrade or downgrade that state.

  • Avoid mutating state: Immutability and create objects that cannot be changed once created. This is the central concept of functional programming

  • Avoid side effects: A side effect is any change in application state that is observable outside of the called function other than its return value. Side effects include Modifying any external variable or object property (for example, a global variable or a variable in the parent function's scope chain), writing to the console, printing something to the screen, writing to a file, sending something to the network , trigger any external process, call any other function with side effects.

Good to know: in a reduced way functional programming can be understood as: the process of building software through the composition of pure functions, avoiding sharing of states, mutable data and side effects. - in case you need to memorize the concept more easily, this is the definition to follow.

3 - What is the difference between class inheritance and prototypal inheritance (classical inheritance and prototypal inheritance)

  • Class inheritance: In classic object-oriented programming languages, objects are abstractions of real-world entities and classes are generalizations (that is, abstractions of objects or other classes). Instances are typically instantiated through constructor functions with the keyword "NEW"
  • Prototypical inheritance:  In prototypical object-oriented programming languages, objects are abstractions of real-world entities or other objects. So a prototype is a generalization. Instances are typically instantiated through factory functions or Object.create(). Instances can be made up of many different objects, allowing for easy selective inheritance.
  • Good to know: Prototypes are the mechanism by which JavaScript objects inherit functionality and/or features from each other.

JavaScript is often described as a prototype-based language to provide inheritance. Objects can include prototype objects that act as model objects that inherit methods and properties. An object's prototype object can also have a prototype object that inherits methods, properties, and so on. This is commonly known as a prototype chain and explains why different objects have properties and methods defined on other objects that are available. - MDN web docs

In JavaScript, prototype inheritance is simpler and more flexible than class inheritance. Have a preference for her

Understood? This concept was the last I learned from javascript and it was also the most complex. But I don't want you to suffer. If you need to memorize a concept, memorize this one: Traditional inheritance is limited to classes that inherit from other classes. However, prototypical inheritance includes not only prototypes inherited from other prototypes, but also objects inherited from prototypes.

learn more about the difference between class inheritance and prototypical inheritance here

4. What are the advantages and disadvantages of functional programming vs object-oriented programming?

Object Oriented Pros:

It's easy to understand the fundamentals of objects and explain the meaning of method calls. Object Oriented tends to use the imperative rather than the declarative. It looks like a series of simple instructions that a computer must follow.

Object-oriented Cons:

Object orientation usually depends on shared state. Objects and behaviors are often grouped into the same entity and any number of functions are accessed randomly in an unspecified order, which can lead to unexpected behaviors such as race conditions.

Pros of functional programming:

Functional models allow programmers to avoid race conditions and side effects, eliminating errors caused by multiple functions competing for the same resource. Features like non-dot type availability (also known as implicit programming) tend to greatly simplify functions and make it easier to recompile into more common reusable code than programming. Object-oriented program. Functional programming also tends to be declarative and declarative. They don't provide step-by-step instructions for the operation, but focus on what to do, so the underlying function needs to do which one. This gives you a lot of freedom in refactoring and performance tuning, allowing you to replace the entire algorithm with a more efficient algorithm with very little code change. (For example, save or use lazy evaluation instead of anxious evaluation.) Processing with pure functions is a multiple processor or distributed process without fear of facing challenges, resource conflicts, conflicts, and so on. It can easily be extended to a cluster. ..

Cons of functional programming:

Excessive use of functional programming features such as pointless style and large outlines can reduce readability because the resulting code is often less abstract, concise, and concrete. Even the everyday language of functional programming can confuse new team members, as many are more familiar with imperative and object-oriented programming than with functional programming. Functional programming has a much steeper learning curve than object-oriented programming. In fact, the prevalence of object-oriented programming has made object languages ​​and learning materials more familiar, while language learning tends to be much more academic and formal. The concept of functional programming is often written about the use of idioms and notations in lambda calculus, algebra, and category theory. All of this requires a prior knowledge base in these areas to be understandable.

5. When is classical inheritance an appropriate choice?

The answer is never or almost never. Certainly never more than one level. Multilevel class hierarchies are an anti-pattern.

If this article was helpful to you, consider subscribing to our newsletter. It takes less than a minute and you'll be kept up to date with articles like this and more. We just need your email: