FrontendDeveloper.in

JavaScript Interview Questions

  • Question 241

    What are the properties of the Intl object

    Below are the list of properties available on the Intl object,

    1. Collator: These are the objects that enable language-sensitive string comparison.
    2. DateTimeFormat: These are the objects that enable language-sensitive date and time formatting.
    3. ListFormat: These are the objects that enable language-sensitive list formatting.
    4. NumberFormat: Objects that enable language-sensitive number formatting.
    5. PluralRules: Objects that enable plural-sensitive formatting and language-specific rules for plurals.
    6. RelativeTimeFormat: Objects that enable language-sensitive relative time formatting.
  • Question 242

    What is an Unary operator

    The unary(+) operator is used to convert a variable to a number.If the variable cannot be converted, it will still become a number but with the value NaN. Let's see this behavior in an action.

    var x = "100";
    var y = +x;
    console.log(typeof x, typeof y); // string, number
    
    var a = "Hello";
    var b = +a;
    console.log(typeof a, typeof b, b); // string, number, NaN
    
  • Question 243

    How do you sort elements in an array

    The sort() method is used to sort the elements of an array in place and returns the sorted array. The default sort order is ascending, based on the string Unicode order. The example usage would be as below,

    var months = ["Aug", "Sep", "Jan", "June"];
    months.sort();
    console.log(months); //  ["Aug", "Jan", "June", "Sep"]
    

    Beware: sort() is changing the original array.

  • Question 244

    What is the purpose of compareFunction while sorting arrays

    The compareFunction is used to define the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

    Let's take an example to see the usage of compareFunction,

    let numbers = [1, 2, 5, 3, 4];
    numbers.sort((a, b) => b - a);
    console.log(numbers); // [5, 4, 3, 2, 1]
    
  • Question 245

    How do you reverse an array

    You can use the reverse() method to reverse the elements in an array. This method is useful to sort an array in descending order. Let's see the usage of reverse() method in an example,

    let numbers = [1, 2, 5, 3, 4];
    numbers.sort((a, b) => b - a);
    numbers.reverse();
    console.log(numbers); // [1, 2, 3, 4 ,5]
    
  • Question 246

    How do you find the min and max values in an array

    You can use Math.min and Math.max methods on array variables to find the minimum and maximum elements within an array. Let's create two functions to find the min and max value with in an array,

    var marks = [50, 20, 70, 60, 45, 30];
    function findMin(arr) {
    return Math.min.apply(null, arr);
    }
    function findMax(arr) {
    return Math.max.apply(null, arr);
    }
    
    console.log(findMin(marks));
    console.log(findMax(marks));
    
  • Question 247

    How do you find the min and max values without Math functions

    You can write functions which loop through an array comparing each value with the lowest value or highest value to find the min and max values. Let's create those functions to find min and max values,

    var marks = [50, 20, 70, 60, 45, 30];
    function findMin(arr) {
    var length = arr.length;
    var min = Infinity;
    while (length--) {
    if (arr[length] < min) {
    min = arr[length];
    }
    }
    return min;
    }
    
    function findMax(arr) {
    var length = arr.length;
    var max = -Infinity;
    while (length--) {
    if (arr[length] > max) {
    max = arr[length];
    }
    }
    return max;
    }
    
    console.log(findMin(marks));
    console.log(findMax(marks));
    
  • Question 248

    What is an empty statement and purpose of it

    The empty statement is a semicolon (;) indicating that no statement will be executed, even if JavaScript syntax requires one. Since there is no action with an empty statement you might think that it's usage is quite less, but the empty statement is occasionally useful when you want to create a loop that has an empty body. For example, you can initialize an array with zero values as below,

    // Initialize an array a
    for (let i = 0; i < a.length; a[i++] = 0);
    
  • Question 249

    How do you get the metadata of a module

    You can use the import.meta object which is a meta-property exposing context-specific meta data to a JavaScript module. It contains information about the current module, such as the module's URL. In browsers, you might get different meta data than NodeJS.

    <script type="module" src="welcome-module.js"></script>;
    console.log(import.meta); // { url: "file:///home/user/welcome-module.js" }
    
  • Question 250

    What is the comma operator

    The comma operator is used to evaluate each of its operands from left to right and returns the value of the last operand. This is totally different from comma usage within arrays, objects, and function arguments and parameters. For example, the usage for numeric expressions would be as below,

    var x = 1;
    x = (x++, x);
    
    console.log(x); // 2
    
  • Question 251

    What is the advantage of the comma operator

    It is normally used to include multiple expressions in a location that requires a single expression. One of the common usages of this comma operator is to supply multiple parameters in a for loop. For example, the below for loop uses multiple expressions in a single location using comma operator,

    for (var a = 0, b =10; a <= 10; a++, b--)
    

    You can also use the comma operator in a return statement where it processes before returning.

    function myFunction() {
    var a = 1;
    return (a += 10), a; // 11
    }
    
  • Question 252

    What is typescript

    TypeScript is a typed superset of JavaScript created by Microsoft that adds optional types, classes and many other features, and compiles to plain JavaScript. Angular is built entirely in TypeScript and it is used as the primary language there. You can install it globally as

    npm install -g typescript
    

    Let's see a simple example of TypeScript usage,

    function greeting(name: string): string {
    return "Hello, " + name;
    }
    
    let user = "Sudheer";
    
    console.log(greeting(user));
    

    The greeting method allows only string type as argument.

  • Question 253

    What are the differences between javascript and typescript

    Below are the list of differences between javascript and typescript,

    featuretypescriptjavascript
    Language paradigmObject oriented programming languageMulti-paradigm language
    Typing supportSupports static typingDynamic typing
    ModulesSupportedNot supported
    InterfaceIt has interfaces conceptDoesn't support interfaces
    Optional parametersFunctions support optional parametersNo support of optional parameters for functions
  • Question 254

    What are the advantages of typescript over javascript

    Below are some of the advantages of typescript over javascript,

    1. TypeScript is able to find compile time errors at the development time only and it makes sures less runtime errors. Whereas javascript is an interpreted language.
    2. TypeScript is strongly-typed or supports static typing which allows for checking type correctness at compile time. This is not available in javascript.
    3. TypeScript compiler can compile the .ts files into ES3,ES4 and ES5 unlike ES6 features of javascript which may not be supported in some browsers.
  • Question 255

    What is an object initializer

    An object initializer is an expression that describes the initialization of an Object. The syntax for this expression is represented as a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). This is also known as literal notation. It is one of the ways to create an object.

    var initObject = { a: "John", b: 50, c: {} };
    
    console.log(initObject.a); // John
    
Get LinkedIn Premium at Rs 399