Javascript Syntax - Javascript tutorial



JavaScript Values

  • The JavaScript syntax defines two types of values.
    • Fixed values
    • Variable values
  • Fixed values are called Literals.
  • Variable values are called Variables.

JavaScript Literals

  • The following are the top two syntax guidelines for fixed values:
    • 1. Numbers are written with or without decimals: 50.60, 5060
    • 2. Strings are text, written within double or single quotes: "kaashiv infotech", 'kaashiv infotech'

JavaScript Variables

  • Variables are used in computer languages to hold data values.
  • JavaScript uses the keywords var, let and const to assign variables.
  • An equal sign is used to assign values to variables.
  • In this example, a is defined as a variable. Then, a is assigned the value 9.

Sample Code

javascript-syntax
<!DOCTYPE html>
<html>

<body>

 <h2>JavaScript Variables</h2>

 <p>In this example, a is defined as a variable.
  Then, a is assigned the value of 9:</p>

 <p id="demo"></p>

 <script>
  let a;
  a = 9;
  document.getElementById("demo").innerHTML = a;

 </script>

</body>

</html>

Output

javascript-variable

JavaScript Operators

  • JavaScript uses arithmetic operators ( + - * / ) to compute values:

Sample Code

javascript-Operators
<!DOCTYPE html>
<html>

<body>

 <h2>JavaScript Operators</h2>

 <p>JavaScript uses arithmetic operators to compute values (just like algebra).</p>

 <p id="demo"></p>

 <script>
  document.getElementById("demo").innerHTML = (4 + 6) * 1000;

 </script>

</body>

</html>

Output

javascript-Operators

Assigning JavaScript Variables

  • JavaScript uses an assignment operator ( = ) to assign values to variables.

Sample Code

javascript-syntax
<!DOCTYPE html>
<html>

<body>

 <h2>Assigning JavaScript Values</h2>

 <p>In JavaScript the = operator is used to assign values to variables.</p>

 <p id="demo"></p>

 <script>
  let a, b;
  a = 5;
  b = 5;
  document.getElementById("demo").innerHTML = a + b;

 </script>

</body>

</html>

Output

javascript-assignment-operator

JavaScript Expressions

  • A set of values, variables, and operators combined to produce a value is called an expression.
  • The computation is called an evaluation.
  • For example, 5 * 5 evaluates to 25:
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Expressions</h2>

<p>Expressions compute to values.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 * 5;
</script>

</body>
</html>

Output

javascript-expressions

JavaScript Keywords

  • JavaScript keywords are used to identify actions to be performed.
  • The let keyword tells the browser to create variables.
<!DOCTYPE html>
<html>
<body>

<h2>The var Keyword Creates Variables</h2>

<p id="demo"></p>

<script>
var x, y;
x = 5 + 6;
y = x * 10;
document.getElementById("demo").innerHTML = y;
</script>

</body>
</html>

Output

javascript-var-keyword

JavaScript Comments

  • Not all JavaScript statements are "executed".
  • Code after double slashes // or between /* and */ is treated as a comment.
  • Comments are ignored, and will not be executed.
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comments are NOT Executed</h2>

<p id="demo"></p>

<script>
let x;
x = 5;
// x = 6; I will not be executed
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output

javascript-comments

JavaScript is Case Sensitive

  • All JavaScript identifiers are case sensitive.
  • The variables lastName and lastname, are two different variables:
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comments are NOT Executed</h2>

<p id="demo"></p>

<script>
let x;
x = 5;
// x = 6; I will not be executed
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output

javascript-case-sensitive

Related Searches to Javascript Syntax - Javascript tutorial