Java script simple statements could be ended without semi colon
Statements:
Both the below statements are valid
var a= 6; var a=5
you could use ‘;’ semicolon to write multiple statement in single line
var1 =10;var 2=23;var 5= 9
Java script is case sensitive, meaning ‘Time’ and ‘time’ are two different elements.
Comments:
//single line comment
/* multi line statement can be made like this */
html comment closing ‘–> ‘ is not detected by Javascript
Data Types:
Primitive:
JS following primitive data types:
- Numbers – 1,2,3 ( Java script does not have separate representation for floating point and whole numbers, all the numeric are represented as floating points (Eg: 2.0)
- Strings – “name”
- Boolean – true or false
also supports ‘null‘ and ‘undefined‘
Composite
JS also so support data type called ‘object‘
Declaring variables:
Variables are elements that store data and helps in re-usability
In JS , variables needs to be declared before it could be used. In JS , variable declaration could be made mainly using three keywords and are :
- var
- let
- const
Note: JS is dynamically typed (untyped) , meaning data type is detected dynamically. For instance, if for below variables , we store username = 1 and password = “we”; then the data type of username will be taken as Number and password as string.
Using Var
var username var password
Multiple variable declaration in a single line is also permitted
var username,password,url
Note:
- Variables declared with var are function scoped.
- Could be modified
- Could be re-declared ( It will replace the previous variable)
- Does support hoisting (meaning that , if the variable is called before declaring , then JS will automatically declare and initialize it with value ‘undefined‘)
console.log(username) var username = "user" // This prints "undefined" because java script changes this code internally as var username = undefined console.log(username) username = "user" //This is called hoisting
Using let:
let username;
Note:
- Variables declared with let are block scoped.
- Could be modified
- Could not be re-declared ( It will replace the previous variable)
- Does not support hoisting. Variable should be declared before it could be used
Using const:
const username;
Note:
- Variables declared with const are block scoped.
- Could not be modified
- Could not be re-declared ( It will replace the previous variable)
- Does not support hoisting. Variable should be declared before it could be used
To know more read : ( Read : https://medium.com/@toddhd/javascript-understanding-the-difference-between-var-let-and-const-d0390da913 )
Variable Scope:
JavaScript only two scope :
- Global – Available anywhere in the code
- Local – Available only to the local function in which its defined
Variable naming rules:
- No reserved keyword are allowed : https://www.tutorialspoint.com/javascript/javascript_variables.htm
- Should start with alphabet or underscore
Operators in JavaScript:
- Arithmetic Operators
+ , – , * , / ,% , ++, —
- Comparison Operators
==, !=, > , <, >=, <=,
- Logical (or Relational) Operators
&& (AND) , || (OR), !
- Assignment Operators
=, += (add and assign), -=, *= ,/=, %=
- Conditional (or ternary) Operators
?:
eg: a==b? true:false
- Bitwise Operators
Note:
Javascript executes statements from left to right so in Javascript
- “a” +9 = “a9“
- “a” + 1 + 5 = “a15”
- 1+5+”a” = “6a“
That is , it considers the element neighboring to a string as string. Here in case 2 , a is string so 1 was considered as string and it gave “a1” string and as “a1” is string 5 was also considered as string which gave the final output a15.
In case 3, 1 was integer so 5 was also considered as integer and the output was 6. Now the operator statement becomes 6+”a”, now 6 is integer but “a” is string, so 6 is also considered as string, resulting in 6a.