Content

  • JavaScript Introduction
  • Where to write JS
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments

JavaScript Introduction

  • JavaScript is mainly used to program the behavior of web pages
  • Using JavaScript, we can change and update HTML and CSS.
  • Javascript frameworks (e.g. - node.js) also can be used to develop dynamic web applications.
  • There are various JS stacks available for full stack Development
  • JavaScript can be use to implement Web API, Web forms API etc.

Where to write JS

  • In HTML, JavaScript code is inserted between <script> and </script> tags.
  • We can create separate JavaScript file (extension - .js)
  • We use <script> tag to give external Javascript reference in HTML page
Example
Output:

Code:
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My text comes from Javascript";
</script>

JavaScript Output

JavaScript can display output in different ways as listed below

  • Output into an HTML element, using innerHTML
  • Output into the HTML document using document.write()
  • Output into an alert box, using alert()
  • Output into the browser console, using console.log()

JavaScript Statements

  • In a programming language, these programming instructions are called statements.
  • A JavaScript program is a list of programming statements.
  • In HTML, JavaScript programs are executed by the web browser.
  • JavaScript statements are composed of Values, Operators, Expressions, Keywords, and Comments
  • The statements are executed, one by one, in the same order as they are written.
  • Semicolons separate JavaScript statements.
Example : JavaScript Statements
let a, b, c;  // Declare 3 variables
a = 5;        // Assign the value 5 to a
b = 6;        // Assign the value 6 to b
c = a + b;    // Assign the sum of a and b to c

JavaScript Syntax

  • JavaScript syntax is the set of rules, how JavaScript programs are constructed.
  • Variable values are called Variables.
  • Strings are text, written within double or single quotes
  • In a programming language, variables are used to store data values
  • JavaScript uses arithmetic operators ( + - * / ) to compute values
  • JavaScript uses an assignment operator ( = ) to assign values to variables

JavaScript Comments

  • JavaScript comments can be used to explain JavaScript code, and to make it more readable.
  • JavaScript comments can also be used to prevent execution, when testing alternative code.
  • Code after double slashes // or between /* and */ is treated as a comment.
Example
Single Line Comments
<script>
// Below code will change content of html tag whose id is heading1:
document.getElementById("heading1").innerHTML = "JavaScript Comments";
</script>
Multi-line Comments
<script>
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
*/
document.getElementById("myH").innerHTML = "JavaScript Comments";
document.getElementById("myP").innerHTML = "My first paragraph.";
</script>