Table of contents
- How to print output on the screen
- How to give user input in JavaScript
- How to add Comments in JavaScript
- Variables:
- Data Types
- Mathematical Operators
- Math Methods
- Type conversion and coercion
- Conditional Operator result is true or false
- Logical Operator
- Truth and Falsy Values
- Increment and Decrement operator
- Ternary Operator
- Conditional Statements
- Loops
- null vs undefined
- Exception Handling
- Block In JavaScript
How to print output on the screen
We use console.log(" ") to print anything in JavaScript
To print the value of a variable we use`as
a console. log(
This is a ${variable}`)
How to give user input in JavaScript
We use prompt()
How to add Comments in JavaScript
// single line, /**/ multiple line comment
Variables:
They are placeholders used to store information There are 3 types of variables in JavaScript var let const let and const are present in the temporal dead zone become active only when they are initialized var is hoisted and the initial value is undefined.
Data Types
Types of Data Types in JavaScript
- Primitive
Objects
Primitive: a.number (of any kind whether it is decimal, float) b.string (a string of any type) c.boolean ( true or false) d. Big Int e.Symbol() ---Introduced in ES6
objects Which store collection of elements array -represented by array[] objects- represented by {}
Introduced in ES6 3.Map It is like an object but the only difference is it is iterable and ordered
4.Set It will only have unique values
Mathematical Operators
- add
- subtract / divide % modulo gives the remainder ** exponent
Math Methods
Math.floor() Rounds up Math.ceil() rounds down
Type conversion and coercion
Type Conversion is explicitly converting the type of data into another the type of() function can be used to check the type of the variable Example: Number(), String()
Type coercion happens when an operator deals with 2 values of different data types. Behind the scenes, JavaScript automatically converts one data type to match the other so the operation can be executed.
Conditional Operator result is true or false
1.Difference between == and === == will check if the value is equal or not irrespective of the data type === will check for both the values as well as the data type also called as strict equality use this always < less than
greater than != this will perform type coercion and will check for value only !== will check for both types as well the value also called as strict!== use this always
Logical Operator
||-or &&-and !-not
Truth and Falsy Values
Falsy Values are false 0 (zero) -0 (minus zero) 0n (BigInt zero) '', "", `` (empty string) null undefined NaN
Everything else is truth
Increment and Decrement operator
a++,a-- ++a,--a
Ternary Operator
condition?if condition is true evaluate this : if condition is false evaluate this part
Conditional Statements
Single if statement if(condition is true){ evaluate }
If else ladder
if(condition true){ evaluate ;} else{ evaluate when if the condition is false }
Switch Statement
switch(variable){ case 1: Perform break default: perform }
if no break written program will free flow from case matched till end in case we should get a value
Loops
while(condition){ evaluate till the condition is false }
for(let i=0;i<value;i++{ evaluate till i is less than the value }
do { will always execute once then will execute only if the condition in the while is true }while(condition)
null vs undefined
null means that value does not exist (here we are implying that the value is defined by its null) undefined means values have not been initialized, by default everything in JavaScript has this state until defined
Exception Handling
try{ // all the code written in this line will be executed until we get an error
} catch(error){ // the moment we get an error code in this block will be executed. }
Block In JavaScript
Anything written inside curly braces is block scoped Example: { // this is block scoped }