javascript rules


Few rules should be known before writing a javascript code which are stated below.

  • semicolon ";" is the symbol that defines the end of a javascript command. So, whenever you need to write multiple lines of javascript codes, you need to separate them with a semicolon.
  • Plain texts that you want to print using javascript must be placed inside the pair of quotation marks which can be either single or double i.e. ' ' or " ".
  • Javascript is case sensitive. It recognizes similar words with different sentence cases as different entities. For example, hello and Hello are different terms for javascript.

Example

<script>
  document.write("<h1>Hello world!!!</h1>");
</script>

document.write()

The example above uses document.write() method which is a javascript command that can be used to print any texts in the web page. It's placed in the middle of the HTML contents instead of placing inside the <head> or right above the </body> markups and is highly useful when you want to display some message to your website users but don't want to use pop up box. It will print the texts wherever you place the code itself. You can also write HTML markups with this command.

Case Sensitivity

As stated earlier, javascript is case sensitive and treats similar entities with different sentencecases like hello and Hello as different terms. Lets see it with examples.

<script>
x = 3;
x = 7;
result = x + x;
document.write(result);
</script>

Result: 14

The first value of variable x is overridden by the second one because both variables are treated as one and in programming the value defined at the last overrides the first one.

<script>
x = 3;
X = 7;
result = x + X;
document.write(result);
</script>

Result: 10

Both variables are recognized as separate entities here. So, the output is the desired one.

The best option while declaring variables would be to go with camel case like javaScriptTutorial while you can opt for any cases but you need to keep consistency within your scripts.