- it is a case sensitive language.
- every statement ends with a semicolon (;).
- variables can be started from an alphabetical digit or an underscore (_).
JavaScript Values
There are two types of values defined in JavaScript:
- variable Values
- fixed Values
Variable Values
Variable values are the variable containers that are used to store the data values. In JavaScript, some specific keywords like let, const, and var are used to declare the variables.
For example, a variable “x” is defined below:
x= 6:
Types of variables
There are two types of variables in JavaScript:
Global variables: Global variables are those which we can access from anywhere in the code. These variables are declared from the outside of the function.
Local variables: Local variables are those which are declared inside a function and cannot be accessed from outside of the function.
var name="Alice";
// Function definition
functionnewFunction() {
// Local variable declaration
var num = 45;
// Display the value of Global variable
document.writeln(Name);
// Display the value of local variable
document.writeln("<br>" + num );
}
// Function call
newFunction();
Fixed Values
Fixed values are known as literals or constant values.
There are two types of literals in JavaScript:
Numbers: Numbers are the numeric values with or without decimals. The syntax of numbers is given below.
28908
Strings: Strings are in text form; this text is written within quotes. The syntax of the string is given below.
'Mark Alice'
JavaScript Operators
In JavaScript, arithmetic operators (‘+,’ ‘-,’ ‘*,’ ‘/’) are used to implement some operations on different operands. The assignment operators (=, %=, +=) are also used. One example is given below based on an arithmetic operator:
var a, b, sum;
// Assign value to the variables
a= 32;
b = 25;
// Use arithmetic operator to
// add both numbers
sum = a + b;
document.write(sum);
Data Types in JavaScript
There are different data types in JavaScript because it holds various types of variables. It is a dynamic programming language; therefore, variable specification is not necessary.
JavaScript use two types of data types:
- Primitive data type: In JavaScript, primitive data is immutable data, which cannot be altered. For example, numbers, strings, boolean numbers, etc.
- Non-primitive data type: In JavaScript, non-primitive data types are objects and methods such as functions and arrays are non-primitive data types.
JavaScript Functions
In JavaScript, functions are the code blocks that are used to perform some specific tasks or operations. These functions are reusable and can reduce the computational cost of a program.
Syntax of a function is given below:
functionfunc() {
// Declare a variable
var num = 15;
// Show the result
document.writeln(num);
}
// Function invoke
func();
JavaScript Keywords
In JavaScript, there are some reserved words with special meanings. Some keywords are given in example:
var a, b;
// function is the keyword that instruct the browser to create a function
function aBc(){};
JavaScript Comments
Comments are part of the code and are ignored during the execution.. We use comments to add the information and suggestions in the code. They increase the readability of code and make the code user-friendly.
In JavaScript, any line between the /* and */ and after double // is considered a comment and not considered during execution.
In the given example the syntax of comments is given:
// x = 5; It will NOT be executed
JavaScript Name Identifiers
In JavaScript, identifiers are used to identify the variable names, functions, and keywords.
These names must start with:
- a dollar sign ($)
- an underscore (_)
- or any letter from the alphabet.
However, numbers at the start of names are not accepted in JavaScript.
Conclusion
JavaScript is a dynamic programming language, which has some defined rules and syntax. To work with JavaScript we should learn the basic concepts of JavaScript such as data types, keywords, functions, name identifiers, etc. In this tutorial, all basic rules and functions of JavaScripts are defined with proper code examples.