JavaScript is a lightweight, robust programming and scripting language executed on a web browser, and it is easy to learn.
1. JavaScript Data Types
Data Types are fundamental to any programming language.
JavaScript can change the content of HTML. It allows three primitive types of data types to work with.
- Boolean(Boolean value true or false)
- Numbers (Numerical value)
- Strings (contain Characters)
JavaScript also defines NULL (represents no value ) and Undefined (represents undefined value), which define a single value.
It also supports a non-primitive data type known as Object (through which one can access members), Array (Collection of similar values), and RegExp (Regular expression), which is a composite data type. All numbers are defined as floating-point numbers using a 64-bit floating-point format in JavaScript. In JavaScript, it does not make a distinction between integers and floating-point numbers.
2. JavaScript Variables
JavaScript also has variables like other programming languages. Variables can be taken as names of storage locations. It has two variable types (i.e., Local Variable and Global Variable).
Note: In JS, a and A are different variables, Because JS(JavaScript) variables are case sensitive.
Before using any variable, we must declare it first and the rules for declaring variables in JS, also called Identifiers.
Name: It starts with a to z (or) A to Z, Underscores (_), or ‘$’ sign.
After the first letter, we use Digits (0 to 9).
JS(JavaScript)Variables are declared with the var keyword.
For example, correct declaration of variables.
var a=10;
var _name=”Aasha”;
Incorrect declaration of Variables.
var 56=30;
var *xx=120;
We can also declare multiple variables with “var” keywords.
var name,money;
Example 2:
<script>
var k=5;
var l=4;
var z=k+l;
document.write(z);
</script>
—> Output : 9
JavaScript Local Variables
- It is declared and accessible within a function or block. Function parameters are local to the function.
- If we declare the same name for a local and global variable. So, you can hide the global variable.
Example:
<script>
function abc(){
var k=10; // local variable
}
</script>
Example:
<script>
if (20<26){
var a=20; // JS local variable
}
</script>
JavaScript Global Variables
- It is declared and accessible from any function (outside the function) or outside a block with a window object called Global variable.
Example:
<script>
var x=100 ; // declared global variable
function xyz(){
document.writeln(x);
}
function abc(){
document.writeln(x);
}
xyz(); //calling JavaScript function
abc(); //calling JavaScript function
</script>
3. JS (JavaScript) Constant
Constants in JS are defined as “const”. The term ‘const’ does not define any constant value. Instead, it defines a constant reference to a value.
- JS (JavaScript), the ‘const’ variable must be assigned a value when declared. Use ‘const’ while declaring Object, Array, RegExp, and Function.
- Variables defined with ‘const’ cannot be redeclared and reassigned, and these are Block-scoped.
- Variables defined with ‘const’ can change the elements of a constant array and the properties of constant objects.
Example:
// const variable cannot be reassigned.
const x=1.234567;
x=1.23; // give an error
x=x+1; // give an error
// const variable must be assigned.
const x= 1.234567; // correctly assigned
// const variable must be assigned.
const x;
x=1.234567; //incorrectly assigned.
4.JS (JavaScript) Strings
JS Strings is a set of characters or sequence of characters used for storing and manipulating text or characters.
There are two ways to make strings in JS.
- String literal (Using Double quotes)
// var string_name=”My name is Thanos”;
- String Object (Using new keyword)
var string_name=new String(‘My name is Thanos”);
// new used to create an instance.
5.JavaScript String Methods
JavaScript String Methods help us to work with strings. JS treats primitive values as objects when executing methods of JS String.
- JS String charAt(index) Method: Returns the character at the given index.
Example:
<script>
var string=”helloworld”;
document.write(string.char(5));
</script>
Output: o
- JS String concat(str) Method: It joins two strings.
Example:
<script>
var string1=”Hello”;
var string2=”world”;
var string3=string1.concat(string2);
document.write(string3);
</script>
Output: ‘Hello world’
- JS String indexOf(str)Method: It Returns index values of given strings
Example:
<script>
var string=”hello world is my first printing string”;
var s1=string.indexOf(“world”);
document.write(s1);
</script>
Output: 6
- JS String lastIndexOf(str)Method: It returns the last index position of the string.
Example:
<script>
var string=”hello world index”;
var s1=string.lastIndexOf(“world”);
document.write(s1);
</script>
Output: 11
- JS String toLowerCase()Method: It returns a string in lowercase.
Example:
<script>
var string=”HELLO WORLD INDEX”;
var s1=string.toLowerCase();
document.write(s1);
</script>
Output: hello world index
- JS String to LowerCase() Method: It returns a string in uppercase.
Example:
<script>
var string=”hello world index”;
var s1=string.toUpperCase();
document.write(s1);
</script>
Output: HELLO WORLD INDEX
- JS String slice (beginIndex, endIndex) Method: It extracts a part of the given string from the starting index to the last index.
The starting index is inclusive, and the end index is exclusive.
Example:
<script>
var string=”helloworldindex”;
var s1=string.slice(5,8);
document.write(s1);
</script>
Output: two // 8th position is exclusive
- JS String trim() Method: It removes the trailing and leading black spaces from both sides of the string.
Example:
<script>
var string=” helloworldindex ”;
var s1=string.trim();
document.write(s1);
</script>
Output: ‘helloworldindex’
- JS String split() Method: It splits the string with blank spaces.
Example:
<script>
var string=”helloworldindex”;
document.write(string.split(“ ”));
</script>
Output: hello world index
6. JS(JavaScript) Numbers
It returns a numerical value, which can be decimal or without the decimal, may be an integer or floating-point.
Example 1 :
// Creating Number object in JS
var s1= new Number(value);
Example 2:
var a=12; //Integer Value
var a=12.456; // floating point
var c=1e12; // Exponent Value
var d=new Number(10); // integer value using Number object
Output: 12 12.456,1000000000000,10
7. Arithmetic Operators
It operates on two numbers, which can be literals, variables, or expressions.
E.g, +,-,*,/,**(exponentiation),%(modulus),++(increment),--(decrement).
Example:
Let a =10+12; //literal
Let c= a+b; //variables
Let c=(a+b)*10; // Expression
- Increment operator: It increments the number by 1. For example,
<script>
let x = 21;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
//the output will be 22
- Decrement operator: It decrements number by 1. For example,
<script>
let x = 21;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
//the output will be 20
8.JS Assignment Operators: It assigns the value to a JavaScript variable.
Example :
=,+=,-=,*=,/=,%=,<<=,>>=,>>>=,&=,^=,|=,**=.
The assignment operator(=): It assigns values to variables.
Example: let a=10;