Artikel Terbaru

Syntax & Comment


Introduction to JavaScript Syntax 
किसी भी HTML program में JavaScript को add करना बहुत ही आसान है। इसके लिए आप <script> tag यूज़ करते है। आप <script> tag को अपने HTML program में कंही भी add कर सकते है।



लेकिन Readability के लिए हमेशा ये suggest किया जाता है की आप इसे <head> tag के अंदर ही लिखे। <script> tags से browser को पता चल जाता है की ये JavaScript code है और इसे कैसे interpret करना है।

<script> tag के 2 attributes होते है। इन attributes के द्वारा आप scripting language और type define करते है।

  • language - इस attribute से आप scripting language define करे है जैसे PHP और JavaScript आदि। 
  • Type - ये attribute necessary होता है इससे आप अपनी file का type define करते है जैसे की text/javascript.


इन attributes की मदद से आप <script> tag को define करते है। इसका उदाहरण नीचे दिया जा रहा है।



<script language="javascript" type="text/javascript">

// यँहा पर आप JavaScript code लिखते है।  

</script>

ये JavaScript define करने का standard syntax है। आप चाहे तो language attribute के बिना भी <script> tag define कर सकते है। लेकिन type attribute add करना अनिवार्य है।



A very first JavaScript Program 

<html>
<head>
<title>First JavaScript program</title>
</head>

<body>

<script type="text/javascript">
document.write("Hello Universe!");
</script>

</body>

</html>
     
उपर दिए गए program में मैने एक HTML file में कुछ JavaScript code add किया है। ये एक बहुत ही simple program है। सबसे पहले <body> section में <script> tag define किया गया है। <script> tag में write function को call किया गया है।



इस function में एक string pass की जाती है। ये function उस string को webpage पर show करता है। इसी तरह आप program में जँहा भी JavaScript add करना चाहते है कर सकते है।



ऊपर दी गयी script निचे दिया गया web page generate करेगी।



first-javascript-program-example-output
          

Adding Comments to a JavaScript Program 

JavaScript में single line और multi-line 2 तरह के comments होते है। इनके बारे में नीचे दिया जा रहा है। 

Single Line Comments 

Single line comments आप double back slash (//) के द्वारा add करते है। इसका उदाहरण नीचे दिया जा रहा है। 

<script type="text/javascript">

// this is a single line comment 

some other JavaScript code here 

</script>
   

Multi-line Comments 

Multi-line comments आप एक back slash और * के द्वारा add करते है। इसका उदाहरण नीचे दिया गया है। 

<script type="text/javascript">

/* This is a 
multi-line comment*/

some other JavaScript code here 

</script>   

Post a Comment

0 Comments