What type of language is JavaScript?

What type of language is JavaScript?

JavaScript is a high-level single-threaded garbage collected interpreted prototype-based multi-paradigm dynamic language with a non-blocking event loop.

But what does all that mean?

  • A high-level programming language is a programming language that is more abstract and is easier for humans to read and write than a low-level programming language.

  • Single threaded means that the JavaScript engine can only execute one piece of code at a time.

      console.time()
    
      for (let i = 0; i < 100000000; i++){
          // do nothing
      }
    
      console.timeEnd()
      // default 5.705s
    

    In the above code, the console would start the time then it will loop through a large number of iterations. While the loop is running, the JavaScript engine will not be able to execute any other code.

  • A garbage collector is a program that automatically frees up memory that is no longer being used by the program. Garbage collection is useful because it helps to prevent memory leaks, which can cause a program to use up more and more memory over time.

  • An interpreted language is a type of programming language in which the source code is not compiled into machine code that can be run directly on a computer. Instead, the source code is interpreted by another program at runtime, which translates the code into instructions that the computer can execute.

  • In a prototype-based language, objects can have properties and methods (functions). An object can inherit the properties and methods of its prototype, and it can also have its own properties and methods. This allows objects to be customized and extended in a flexible way.

  • A multi-paradigm programming language is a language that supports more than one programming paradigm, or approach to programming like object-oriented programming, functional programming, imperative programming, etc.

  • In a dynamic language, the programmer does not need to specify the type of a value when declaring a variable or a function. This makes writing code easier because you don't have to wait to declare the type of variable when it's declared. But in the long run the freedom and wild nature of JavaScript can work against you making it more difficult to debug and maintain code.

  • Event loop is a mechanism that allows the program to execute asynchronous code in a non-blocking manner. This feature is important in the context of JavaScript. Since JavaScript is a single-thread language, if a piece of code takes a long time to run, it can block the execution of other code which can make the program appear unresponsive.

      console.log('Start')
    
      setTimeout(() => {
          console.log('Timeout expired')
      }, 1000);
    
      // Console
      // Start
      // End
      // Timeout expired
    

    In this code, the setTimeout() function is used to set a timer that will expire after 1000 milliseconds (1 second). When the timer expires, it will generate an event that will be added to the event queue. The program does not stop or block when the setTimeout() function is called, but instead continues to run and processes the event when it becomes available.

    And that's a wrap. Thank you for taking the time to read this article. I hope you found it informative and valuable. If you have any thoughts or insights on the topic, please feel free to share them in the comments below. If you'd like to connect or learn more about my work, please don't hesitate to reach out. Thanks again for reading and have a great day!