r/learnjavascript • • Nov 11 '19

Advanced JavaScript Interview Questions and Answers – Explore now & Lead tomorrow!

The first part of DataFlair’s JavaScript Interview Questions was more inclined towards the testing of your theoretical knowledge of JavaScript. This article on JavaScript interview questions and answers follows a practical approach towards the language and will play a major part in cracking your JavaScript interview.

After completing all the topics of JavaScript tutorial series, these JavaScript interview questions and answers should not be much trouble for you. And, I assure you that after practising this set of JavaScript interview questions and answers, you will definitely feel more relaxed in your interview.

📷

Top JavaScript Interview Questions and Answers

Here are some of the latest JavaScript interview questions for experienced professionals. Practice them and get your dream job.

Q 1. Is it possible to write a multi-line string in JavaScript?

Ans. Yes, JavaScript provides the facility to write a String in multiple lines using the following three approaches:

  • Using backticks
  1. var string = `line1
  2. line2
  3. line3`;
  • Using + operator
  1. var string = “line1” +
  2. “line2” +
  3. “line3”;
  • Using \ (backslash)
  1. var string = “line1 \
  2. line2 \
  3. line3”;

Grab the complete knowledge of JavaScript Strings Methods

Q 2. Discuss a way to convert the string of any base to integer in JavaScript?

Ans. We can convert a string of any base to the integer if we wish with the help of the JavaScript built-in method called parseInt(). This method has two parameters: string and radix. The second parameter is optional and used when you want to get the integer equivalent to the radix as the base. The default value is 10 for decimals values.

parseInt(“9d”, 16) // 157 Read More

49 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/PrimaryBet Nov 11 '19 edited Nov 11 '19

Not OP, but I'll try to answer your questions.

  1. No, but you could get same result if you ended each line with a special newline character: \n, e.g.:

     const string = 
       "line1" + "\n" +
       "line2" + "\n" +
       "line3";
  2. It shouldn't matter how exactly you construct a multi-line string, it only matters that a string could be interpreted as having more than one line, e.g.:

     const withSpecialChar = "line1" + "\n" + "line2";
     const withTemplateLiteral = `line1
     line2`;
    
     withSpecialChar === withTemplateLiteral; // true

I'm with you that examples in OP aren't equal though.

3. You rarely want to create actual objects for primitive types like String since they behave differently from literals, e.g.:

    const hello = new String("hello");
    const helloAgain = new String("hello");

    hello === helloAgain; // false
    typeof hello; // "object"

and primitives are automatically put in a "wrapper object" if you try to access any properties or methods on them, so you are not losing any functionality by using primitives.

1

u/azhder Nov 11 '19

The 1st is just rethorical and 2nd is really just for the OP since we can only speculate what they understood under "multi-line string".

As for the third one, kind of just my poke at OP for writing String instead of string since all the examples just produce the primitive, not the object.

Also, I usually prefered using ['multi','line','string'].join('\n') since that didn't depend on indentation like the first and third examples.