Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Development

    Enhancing Software Performance: Strategies for Optimal Speed

    Programming

    С днем программиста!

    JavaScript

    This Is How I Created a Simple App Using React Routing

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / 20 JavaScript Interview Questions – Part 2
    Interview

    20 JavaScript Interview Questions – Part 2

    Marina VorontsovaBy Marina VorontsovaMarch 15, 2019Updated:December 6, 2024No Comments12 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    20 JavaScript Interview Questions – Part 2
    30 JavaScript Interview Questions - Part 2 | Theory and Practice
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    30 JavaScript Interview Questions - Part 2 | Theory and Practice
    30 JavaScript Interview Questions – Part 2 | Theory and Practice

    We continue the series with JavaScript interview questions, where we cover basic theory that you might be asked during a technical interview, as well as look into practical problem sets that will prepare you for coding challenges. If you’ve missed Part 1, then here it is – JavaScript Questions Part 1. Without further ado, let’s get started.

    Theory
    Practice

    On a side note, it’s worth mentioning, that everything in this article is written using ES5, for ES6+, please, check back within a couple of weeks for more questions written using ES6 syntax.

    Theory

    What are the Primitive and Reference types in JavaScript? What are the most important differences between the two?

    Primitives are simple types, like String, Number, or Boolean. Reference types are more complex and include Array and Object.

    The most important difference can be observed in copying variables. For example, in copying a primitive (a variable holding a number), the new variable will be an actual copy, meaning if you change the first variable, the second variable (holding a copy) will not be changed. Conversely, in reference types, the variables do not store data, but rather — a pointer to a place in memory where that data is stored. Thus, when copying such a variable, you’re actually copying a pointer, and when you change the value of the first variable, the value of the second one will also be changed, because you changed the data in the memory, while pointer stays the same.

    For example,

    var number1 = 20;
    var number2 = number1;
    number1 = 15;
    console.log(number1); // → 15
    console.log(number2); // → 20

    Conversely,

    var array1 = [1, 2, 3];
    var array2 = array1;
    array1.push(4);
    console.log(array1); // → [1, 2, 3, 4]
    console.log(array2); // → [1, 2, 3, 4]

    What’s the difference between global and local scope?

    By default, all variables, objects, and functions in JavaScript belong to the global scope. When executing code in the browser, that scope is the Window object. Because of that, if you change the variable, it will be changed across all code. The functional or local scope is when you write a function and put its own scope inside the curly braces; inside those braces, you may use the same variable names as in the global scope without interfering with the rest of your code. Variables in the local scope won’t be seen in the global scope.

    For example,

    var carName = "Volvo";
    // code here can use carName
    function myFunction() {
      // code here can also use carName
      var carColor = “red”;
    }
    // code here can not use carColor

    What are the common ways to create objects in JavaScript? Provide examples.

    There are different ways to create objects in JavaScript. Among the three common ones are:

    First method (literal notation):

    var myObj = {
      value1: ‘a value’,
      Fn: function() {...}
    }

    Second method:

    var country = new Object ();
    Contry.name = ‘Italy’;

    Third method:

    var house = Object.create(null);
    house size = 45;

    Fourth method (constructor functions):

    var Person = function() {
      this.name = '';
      this.greet = function() {
        console.log('Hi, my name is ' + this.name + ' and I am ' + this.age + ' years old!');
      }
    };
    var max = new Person();

    What is this in JavaScript? And how can you control the value of this? Provide examples.

    this is a very important keyword in JavaScript that allows adding properties to an object. You can control the value of this with the bind(), call() and apply() methods, which allow you to overwrite the default this value.

    For example,
    call attaches this into a function and executes the function immediately:

    var person = {
      name: "Jesus Christ",
      hello: function(thing) {
        console.log(this.name + " says hello " + thing);
      }
    }
    person.hello("world"); // → "Jesus Christ says hello world"
    person.hello.call({ name: "Virgin Mary" }, "world"); // → "Virgin Mary says hello world"

    bind attaches this into a function and it needs to be invoked separately like this:

    var person = {
      name: "Jesus Christ",
      hello: function(thing) {
        console.log(this.name + " says hello " + thing);
      }
    }
    person.hello("world"); // → "Jesus Christ says hello world"
    var helloFunc = person.hello.bind({ name: "Virgin Mary" });
    helloFunc("world"); // → Virgin Mary says hello world"

    apply is similar to call except that it takes an array-like object instead of listing the arguments out one at a time:

    function personContainer() {
      var person = {
        name: "Jesus Christ",
        hello: function() {
          console.log(this.name + " says hello " + arguments[1]);
        }
      }
      person.hello.apply(person, arguments);
    }
    personContainer("world", "mars"); // → "Jesus Christ says hello mars", note: arguments[0] = "world" , arguments[1] = "mars"

    What are closures? Provide an example.

    A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

    Read More:  Mastering Common Interview Questions: A Guide to Effective Responses

    For example,

    function getCounter() {
      var count = 0;
    
      return function() {
        return count += 1;
      }
    }
    
    var add = getCounter();
    
    // Call add() 3 times
    add();
    add();
    add();
    
    // → the count is now 3

    In this example, add is assigned the return value of getCounter function, sets the count to zero, and returns a function expression. Now add becomes a function that is able to access the counter in the parent scope. Now, this is called closure, which makes it possible for a function to have private variables.

    30 JavaScript Interview Questions - Part 2 | Theory and Practice

    How can you “schedule a call” or execute a function at a later time? Provide examples.

    There are two methods to execute a function at a later time: by using setTimeout and setInterval

    setTimeout allows running a function once after the interval of time.

    setInterval allows running a function regularly with the interval between the runs.

    However, these methods are outside JavaScript Specifications. But nevertheless, most environments have an internal scheduler and provide those functions (like in Node.js).

    For example,

    function sayHi() {
      alert('Hello');
    }
    
    setTimeout(sayHi, 1000); // → calls sayHi() in one second

    And:

    let timerId = setInterval(function() {
      alert('tick');
    }, 2000); // → repeat with the interval of 2 seconds
    
    setTimeout(function() {
      clearInterval(timerId);
      alert('stop');
    }, 5000); // → after 5 seconds stop

    What are the Event Listeners and why you need them?

    The addEventListener() method attaches an event handler to the specified element without overwriting existing event handlers. You can add as many event handlers to one element as you want, even if they are of the same type. The addEventListener() method makes it easier to control how the event reacts to bubbling. Syntax: element.addEventListener(event, function, useCapture);

    Example:

    element.addEventListener("mouseover", myFunction);
    element.addEventListener("click", mySecondFunction);
    element.addEventListener("mouseout", myThirdFunction);

    What is AJAX and how it works?

    AJAX stands for Asynchronous JavaScript and XML. AJAX uses a combination of browser built-in XMLHttpRequest object (to request data from a web server) and JavaScript and HTML DOM (to display or use the data). AJAX can transport data either as XML or as plain text or JSON. AJAX workflow can be represented as follows: An event occurs on a web page, an XMLHttpRequest object is created by JavaScript, the XMLHttpRequest object sends a request to a web server, the server processes the request and sends a response back to the website, the response is then read by JavaScript and proper action is executed by JavaScript.

    What is JSON and why you need one? What are the differences and similarities between JSON and XML?

    JSON stands for JavaScript Object Notation and is defined as a syntax for storing and exchanging data. When the data is exchanged between browser and server, it can only be text, JSON is exactly that, and any JavaScript object can be converted to JSON and visa versa, JSON back to JavaScript object.

    Similarities with XML: both are human readable and self-describing, hierarchical (values within values), can be both parsed and used by many programming languages, and can be fetched with an XMLHttpRequest

    Differences: JSON can be parsed by a standard JavaScript function, while XML has to be parsed with an XML parser. JSON is much shorter, quicker to write and read, can use arrays and doesn’t use end tag.

    What is jQuery and should you use it in 2019?

    JQuery is a JavaScript library that was first created to handle Browser Incompatibilities and to simplify HTML DOM Manipulation, Event Handling, Animations, and Ajax. JQuery takes a lot of complicated JavaScript tasks and simplifies them into methods that you can call with a single line of code.

    Read More:  MongoDb. Capped collections

    It used to be pretty popular back in the day, but now some of the needs for jQuery has been superseded by modern browsers and frameworks. However, this library is still in use by a lot of developers. Today, Selectors, API, and Fetch have been standardized to the browser, so the need for jQuery is declining rapidly.

    Practice

    Write a JavaScript program to create a Clock.

    Sample Output (will come every second):
    “11:17:22”
    “11:17:23”
    “11:17:24”

    Answer

    function pad(symb) {
      return String(symb).length == 1 ? '0' + symb : symb;
    }
    function clock() {
      var time = new Date(),
        hours = time.getHours(),
        minutes = time.getMinutes(),
        seconds = time.getSeconds();
      return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
    }
    setInterval(function() {
      console.log(clock());
    }, 1000);

    Write a JavaScript function to create random hex color.

    Sample Output: “#62c0d8”

    Answer

    // using function pad from previous task
    function randHex() {
        return pad(Math.floor(Math.random() * 256).toString(16));
    }
      function randHexColor() {
        return '#' + randHex() + randHex() + randHex();
    }
    console.log(randHexColor());

    Write a JavaScript function that accepts a number as a parameter and check if the number is prime or not.

    Note : A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

    Answer

    function isPrime(n) {
      if (n === 1) {
        return false;
      } else if (n === 2) {
        return true;
      } else {
        for (var i = 2; i < n; i++) {
          if (n % i === 0) {
            return false;
          }
        }
        return true;
      }
    }
    console.log(isPrime(23));

    Write a JavaScript program to remove duplicate items from the char array.

    Sample Input: [‘d’, ‘3’, ‘a’, ‘d’, ‘d’, ‘c’, ‘c’, ‘3’, ‘1’, ‘a’, ‘d’]
    Sample Output : [‘1’, ‘3’, ‘d’, ‘a’, ‘c’]

    Answer

    function removeDuplicates(num) {
      var x,
        len = num.length,
        out = [],
        obj = {};
      for (x = 0; x < len; x++) {
        obj[num[x]] = 0;
      }
      for (x in obj) {
        out.push(x);
      }
      return out;
    }
    console.log(removeDuplicates(['d', '3', 'a', 'd', 'd', 'c', 'c', '3', '1', 'a', 'd']));

    Write a JavaScript program to find the most frequent item of an array.

    Sample Input:[‘d’, ‘3’, ‘a’, ‘d’, ‘d’, ‘c’, ‘c’, ‘3’, ‘1’, ‘a’, ‘d’]
    Sample Output : “d: 4”

    Answer

    function mostFrequent(arr) {
      var obj = {},
        symb = '',
        max = 0;
      for (var i = 0; i < arr.length; i++) { 
        if (typeof obj[arr[i]] === 'undefined') {
          obj[arr[i]] = 1; 
        } else {
          obj[arr[i]] += 1;
        } 
      } 
      Object.keys(obj).forEach(function(key) {
        if (obj[key] > max) {
          symb = key;
          max = obj[key];
        }
      });
      return symb + ': ' + max;
    }
    console.log(mostFrequent(['d', 3, 'a', 'd', 'd', 'c', 'c', 3, 1, 'a', 'd']));

    Write a JavaScript function to generate an array between two integers of 1 step length.

    Sample Input 1: -3, 5
    Sample Output 1: [-3, -2, -1, 0, 1, 2, 3, 4, 5]
    Sample Input 2: 5, -3
    Sample Output 2: [5, 4, 3, 2, 1, 0, -1, -2, -3]

    Answer

    function getRange(start, end) {
      var arr;
      if (start > end) {
        arr = new Array(start - end + 1);
        for (var i = 0; i < arr.length; i++, start--) {
          arr[i] = start;
        }
      } else {
        arr = new Array(end - start + 1);
        for (var j = 0; j < arr.length; j++, start++) {
          arr[j] = start;
        }
      }
    return arr;
    }
    console.log(getRange(-3, 5));
    console.log(getRange(5,-3));

    Write a JavaScript program to find the greatest common divisor of two positive numbers.

    Sample Input: 32, 24
    Sample Output: 8

    Answer

    function gcd(a, b) {
      return b ? gcd(b, a % b) : a;
    }
    console.log(gcd(32, 24));

    Write a JavaScript program to get the first n Fibonacci numbers.

    Note : The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . Each subsequent number is the sum of the previous two.
    Sample Input: 7
    Sample Output: [0, 1, 1, 2, 3, 5, 8]

    Answer

    var fibonacci = function(n) {
      if (n === 1) {
        return [0];
      } else if (n === 2) {
        return [0, 1];
      } else {
        var s = fibonacci(n - 1);
        s.push(s[s.length - 1] + s[s.length - 2]);
        return s;
      }
    };
    console.log(fibonacci(7));

    Write a JavaScript function to round down an integer value to the previous multiple of 5.

    Sample Input 1: 21
    Sample Output 1: 20
    Sample Input 2: 99
    Sample Output 2: 95

    Answer

    function floor5(num) {
      return Math.floor(num / 5) * 5;
    }
    console.log(floor5(21));
    console.log(floor5(99));

    Write a JavaScript function to check that two words are anagrams (formed by rearranging the letters of another).

    Answer

    function checkAnagrams(a, b) {
      return a.split('').sort().join('') === b.split('').sort().join('');
    }
    console.log(checkAnagrams('cinema', 'iceman'));

    Conclusion

    Please, come back a few weeks later for the continuation of the series, where we cover ES6 (and above) JavaScript interview questions.

    interview questions JavaScript javascript coding challenge javascript interview javascript interview questions javascript programming
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Marina Vorontsova
    • Website

    Related Posts

    Mastering Common Interview Questions: A Guide to Effective Responses

    December 19, 2024

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Interview December 10, 2019

    Tips and Tricks for a Newbie at Web Development: How to Prepare for the Interview

    Technical questions are unavoidable for developers as you look for your programming job interviews. The way you handle these questions can help or hurt your chance of working with that company.

    Effective Strategies for Acing Part-Time Job Interviews

    November 30, 2024

    Implementing Data Privacy Principles in Software Development

    December 4, 2024

    Essential Modernising our Talent Programme

    January 22, 2019

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Essential Contributions of Backend Development in Full-Stack Projects

    Development December 6, 2024

    Optimizing LinkedIn: A Strategic Lead Generation Funnel Approach

    LinkedIn November 27, 2024

    Strategies to Mitigate Duplicate Content in Programmatic SEO

    SEO & Analytics August 27, 2025

    Project Manager Role

    JavaScript June 1, 2016

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Content & Leadership

    Analyzing Storytelling’s Impact on Content Marketing Effectiveness

    Programming

    8. Уроки Node.js. Наследование от ошибок Error

    LinkedIn

    Maximizing Lead Generation with LinkedIn InMail Strategies

    Most Popular

    Create a Simple POS with React, Node and MongoDB #0: Initial Setup Frontend and Backend

    JavaScript

    Leveraging Interactive Content for Effective B2B Lead Generation

    B2B Leads

    Strategies to Boost B2B Lead Conversion Rates Effectively

    B2B Leads
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.