0

I am executing following code

console.log('hello');
console.log('hello 2')

In above code why missing a semicolon does not throw an error?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anam Nizami
  • 377
  • 1
  • 4
  • 22
  • Because semicolons in Javascript are optional. – Jordi Nebot Nov 18 '17 at 10:34
  • [*"JavaScript applications consist of statements with an appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon."*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) – axiac Nov 18 '17 at 10:49

1 Answers1

2

Because that is automatically inserted by compiler. Semicolon is optional in JavaScript and added to maintain code readability and termed as good coding practice. Please note, if two statement are in one line then semicolon is not optional, and you need to put it over there. like below

var i = 0; i++  
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25
  • In the second statement you contradict what you said in the first statement. The semicolon in Javascript separates two statements on the same line. It is not optional. – axiac Nov 18 '17 at 10:48
  • Yes, I said the same thing. Semicolon in not optional in case of same line. – Vipin Kumar Nov 18 '17 at 11:17