Variables in JS
Variables are containers that store values. Variable names can be any legal identifier.
You can start by declaring a variable with the var (less recommended) or let keyword, followed by the name you give to the variable.
Important point to be remembered about Variables.
Important points to remember about variables.
var is the keyword to declare a variable.
Variables are the identifiers to store data values.
‘=’ is used to assign a value to a variable.
Variable names must start with _ or $ or letter.
99temp is invalid, whereas _99temp is valid.
What is ECMAscript ?
ES2015(known as ECMAscript), two other ways to declare variables were introduced- let and const. We can use these 2 keywords to define variables(now recommended).
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
Operators in JS
We are going to explore these operators.
Variables can be declared either by key var and value OR simply by assigning values directly.
var x = 42;
x = 42 ;
We can assign the values of different data types to the same variable.
var x = 34;
var x = “Neptune”;
Type of a variable can be checked by using typeof operator.
var x = "Neptune";
console.log(typeof x); //returns String
What is the difference between == and === operators ?
3.Standard Arithmetic operators
Addition + Ex: [5 + 8]
Subtraction - Ex: [49 – 38]
Division / Ex: [ 49 / 7]
Multiplication * Ex: [28 * 2]
Modulus % to return the remainder of a division – Ex: 50 % 7 is 1
Increment ++ to increment the operand itself by 1 – Ex: If x=4, x++ evaluates to 5
Decrement -- to decrement the operand itself by 1 – Ex: if x= 10, x—- evaluates to 9
AND &&, OR ||, NOT ! are the logical operators often used during conditional statements to test logic between variables.
Expr1 && Expr2 returns true if both are true, else returns false.
Expr1 || Expr2 returns true if either is true.
!Expr1 operates on a single operand to convert true to false and vice versa.
Operator "+" is used to concatenate strings.
var x = "Hello";
var y = " World ";
console.log(x + y);
/* Returns “Hello World” */
var x = "Hello";
var y = 100;
var z = 333;
console.log(x + y + z);
/* Returns “Hello100333” */