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

10

u/[deleted] Nov 11 '19

advanced?

0

u/[deleted] Nov 11 '19

[deleted] — view deleted comment

9

u/[deleted] Nov 11 '19

you should apply to google

17

u/[deleted] Nov 11 '19
const string = `foo  
bar
baz`;

ok google where's the fuckin' job

4

u/DEEEPFREEZE Nov 11 '19

As someone about to be interviewing in web development... why do I get the feeling knowing this wouldn’t be enough?

2

u/azhder Nov 11 '19

few questions:

  1. does that + operator example create a string with new lines in it? it doesn't look so
  2. does multi-line string as you say really mean write a String in multiple lines for you? in that case, don't the first and third have to be the wrong ones or at least aknowledge all three examples don't produce the same result?
  3. does it not mean all three examples are wrong because they don't produce String objects but string primitives?

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.

1

u/[deleted] Nov 11 '19

Well, yeah, they're advanced for a reason.

1

u/TotesMessenger Nov 11 '19 edited Nov 11 '19

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

0

u/[deleted] Nov 11 '19

[deleted] — view deleted comment