JavaScript | Syntax - GeeksforGeeks

20 Nov, 2021
JavaScript is a 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: 
Javascript
<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 a window object.
Example:  
Javascript
<script> // Declare a variable and initialize it// Gloabal variable declarationvar Name="Apple";   // Function definitionfunction 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 callMyFunction();  </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:  
Javascript
<script> // Variable Declarationsvar x, y, sum; // Assign value to the variablesx = 3;y = 23; // Use arithmetic operator to// add two numberssum = 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:  
Javascript
<script> // Variable Declarationsvar x, num, sum; // Assign value to the variablesx = 20;y = 30 // Expression to divide a numbernum = x / 2; // Expression to add two numberssum = 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 increases 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:  
Javascript
<script> // Variable Declarationsvar x, num, sum; // Assign value to the variablesx = 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 Student = {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:  
Javascript
<script>   // Function definitionfunction func() {          // Declare a variable    var num = 45;                          // Display the result    document.writeln(num);    }  // Function callfunc(); </script>
Output: 
45
 
shubham_singh
kapoorsagar226
javascript-basics
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript