Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . JavaScript Example

JavaScript Example

 Javascript example is easy to code. JavaScript provides 3 places to put the JavaScript code: within body tag, within head tag and external JavaScript file.

Let’s create the first JavaScript example.

  1. <script type="text/javascript">  
  2. document.write("JavaScript is a simple language for javatpoint learners");  
  3. </script>  
Test it Now

The script tag specifies that we are using JavaScript.

The text/javascript is the content type that provides information to the browser about the data.The document.write() function is used to display dynamic content through JavaScript. We will learn about document object in detail later.


3 Places to put JavaScript code

  1. Between the body tag of html
  2. Between the head tag of html
  3. In .js file (external javaScript)

1) JavaScript Example : code between the body tag

In the above example, we have displayed the dynamic content using JavaScript. Let’s see the simple example of JavaScript that displays alert dialog box.

  1. <script type="text/javascript">  
  2.  alert("Hello Javatpoint");  
  3. </script>  
Test it Now

2) JavaScript Example : code between the head tag

Let’s see the same example of displaying alert dialog box of JavaScript that is contained inside the head tag.

In this example, we are creating a function msg(). To create function in JavaScript, you need to write function with function_name as given below.

To call function, you need to work on event. Here we are using onclick event to call msg() function.

  1. <html>  
  2. <head>  
  3. <script type="text/javascript">  
  4. function msg(){  
  5.  alert("Hello Javatpoint");  
  6. }  
  7. </script>  
  8. </head>  
  9. <body>  
  10. <p>Welcome to JavaScript</p>  
  11. <form>  
  12. <input type="button" value="click" onclick="msg()"/>  
  13. </form>  
  14. </body>  
  15. </html>  
Test it Now




Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.