Skip to content
Related Articles

Related Articles

JavaScript | Syntax
  • Last Updated : 09 May, 2019

JavaScript is the lightweight and dynamic computer programming language. It is used to create client side dynamic pages. It is open source and cross-platform language.

Basic Syntax:




<script>  
document.write("Basic Print method in JavaScript");  
</script>  

JavaScript syntax refers to the set of rules that determines how JavaScript programs are constructed:

// Variable declaration
var c, d, e;

// Assign value to the variable
c = 5; 

// Computer value of variables
d = c;
e = c/d;

JavaScript Variables: A JavaScript variable is the simple name of storage location where data to be stored. There are two types of variables in JavaScript which are listed below:

  • Local variables: Declare a variable inside of block or function.
  • Global variables: Declare a variable outside function or with window object.

Example:






<script>
  
// Declare a variable and initialize it
// Gloabal variable declaration
var Name="Apple";   
  
// Function definition
function MyFunction() {  
      
    // Local variable declaration
    var num = 45;    
      
    // Display the value of Gloabal variable
    document.writeln(Name);
      
    // Display the value of local variable
    document.writeln("<br>" + num );
}  
  
// Function call
MyFunction();
   
</script>

Output:

Apple
45

JavaScript Operator: JavaScript operators are symbols that are used to compute the value or in other word we can perform operations on operands. Arithmetic operators ( +, -, *, / ) are used to compute the value and Assignment operator ( =, +=, %= ) are used to assign the values to variables.

Example:




<script> 
  
// Variable Declarations
var x, y, sum;
  
// Assign value to the variables
x = 3;
y = 23;
  
// Use arithmetic operator to
// add two numbers
sum = x + y;
  
document.write(sum);
  
</script>

Output:

26

JavaScript Expression: Expression is the combination of values, operators and variables. It is used to compute the values.

Example:




<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
// Expression to divide a number
num = x / 2;
  
// Expression to add two numbers
sum = x + y;
  
document.write(num + "<br>" + sum);
  
</script>

Output:

10
50

JavaScript Keyword: The keywords are the reserved words that have special meaning in JavaScript.



// var is the keyword used to define the variable
var a, b;

// function is the keyword which tells the browser to create a function
function GFG(){};

JavaScript Comments: The comments are ignored by JavaScript compiler. It increase the readability of code. It adds suggestions, Information and warning of code. Anything written after double slashes // (single line comment) or between /* and */ (multi line comment) is treated as comment and ignored by JavaScript compiler.

Example:




<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
/* Expression to add two numbers */
sum = x + y;
  
document.write(sum);
  
</script>
50

JavaScript Data Types: JavaScript provides different datatype to hold different values on variable. JavaScript is a dynamic programming language, it means do not need to specify the type of variable. There are two types of data types in JavaScript.

  • Primitive data type
  • Non-primitive (reference) data type
// It store string data type
var txt = "GeeksforGeeks";

// It store integer data type
var a = 5;
var b = 5;

// It store Boolean data type
(a == b )
 
// It store array data type
var places= ["GFG", "Computer", "Hello"];

// It store object data
var Stdent = {firstName:"Johnny", lastName:"Diaz", age:35, mark:"blueEYE"}

JavaScript Functions: JavaScript functions are the blocks of code used to perform some particular operations. JavaScript function is executed when something call it. It calls many times so the function is reusable.

Syntax:

function functionName( par1, par2, ....., parn ) {  
    // Function code
}  

JavaScript function can contain zero or more arguments.

Example:




<script>
    
// Function definition
function func() {  
      
    // Declare a variable
    var num = 45;                  
      
    // Display the result
    document.writeln(num);     
}  
  
// Function call
func();
  
</script>

Output:

45

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :