Java Script
Used for DOM
Event handling
Asynchronous Requests
Animations and Effects
Data Manipulation (sorting, filtering)
Storing Data
Single Page Applications (SPA)
Creating APIs and Web Services
Console.log – where console is the object and log is a method and parenthesis is used to
print.
Various methods with console.
Example
alert("Hello, World! from first.js")
console.log("Hello, World! from first.js" )
console.log(20,"hello",true)
const x=100
console.log(x)
console.error("This is an error")
console.warn("This is a warning")
console.table({'Core Java':"mangesh Wayal","Ashish Jadhav":"Advance Java"})
this will group all the below statements
console.group('simple')
console.log("Hello")
console.error("Error")
console.warn(x)
console.groupEnd('simple')
const style="color:blue;font-size:20px"
console.log("%cHello, World! from first.js",style)
Comment in javascripts
//this is the comment
/* multiline comment
*/
Variables
We use var, let and const.
Var is the tradition variable declaration after the 2015 new variables declaration is
introduces.
Let and const introduce later Due to scope
Naming conventions- only letters, numbers, underscore, and dollar, signs
Can’t start with a number.
Const- const cannot reassigned. Directly changing the value is not allowed but we can
append values in const array. We can add or change the value of name in object.
We assign multiple values at the same time.
Data Types
Primitive data types
String- sequence of characters
Numbers- Integer as well as floating-point numbers
Boolean- logical entity/true or false.
Null- intentional absence of any object value.
Undefined- a variable that has not yet been defined/assigned
Symbol- Built-in object whose constructor returns a unique symbol
BigInt- Number that are greater than the “number” type can handle
Reference type (objects)
Reference type or objects are non-primitive value and when assigned to a
variable, the variable is given a reference to that value.
Object literals, arrays and functions are all reference types.
Static vs Dynamic Types
Javascript is a dynamic-typed language. this means we do not explicitly
defines the types for our variables. Many other languages are statically-typed
such as c,c++, Java.
TypeScript is superset of JavaScript, which allows static-typing. This can make
your code more verbose and less prone to errors. TypeScript may be something
you want to look into later on.
Why null data type returns object when we check its type ?
- In the first implementation of the JavaScript, JavaScript values were
represented as a type tag and value. The type tag for object was 0. Null
was represented as the Null pointer (0x00 in most platforms).
- Consequently, null had 0 as type tag, hence the typeof return value ,
“object”.
Primitive Types: Stored directly in the “stack”, where it is accessed from
String, number, Boolean, Null, Undefined, symbol , Bigint
Reference Types- Stored in the head and accessed by reference
Arrays, Functions, Objects
Type conversion
let a="100"
// a=+a //we can also use this method for type conversion
a=parseInt(a) //this is usual method of type conversion
a=Number(a) //this is another way of type conversion
console.log(a,typeof a)
//change number to string
b=100
b=b.toString();
console.log(b,typeof b)
// convert to decimal
c=100
c=parseFloat(c)
console.log(c,typeof c)
//convert to boolean
//0 is the only number which is false
c=Boolean(c)
console.log(c,typeof c)
console.log(Math.sqrt(-1))
console.log(1+NaN)
console.log(undefined+undefined)
console.log('foo'/3);
Operators
// 1. Arithmetic operators
let x;
x=5+5
console.log(x)
x=5-5
console.log(x)
x=5*5
console.log(x)
x=5/5
console.log(x)
x=6%5
console.log(x)
//concatination
x='Hello'+' hi'
console.log(x)
// exponent
x=2**3
console.log(x)
//increment
x++
console.log(x)
//decrement
x--
console.log(x)
// 2. Assignment Operators
x=10
console.log(x)
x+=5
console.log(x)
x-=5
console.log(x)
x*=5
console.log(x)
x/=5
console.log(x)
x**=5
console.log(x)
x%=5
console.log(x)
// 3. Comparison Operators
x=2 =='2' //this only check the value
console.log(x)
x=2===2 //it also check the data type
console.log(x)
x=2 !='2'
console.log(x)
y=2 !=='2' //this also check the data type, so it returns true bacuase 2 is
not equal to '2'
console.log(y)
Type Coercion
if we write a program to add one number and string then number is converted
to the string and it get concatenated
ex.
let x;
x=5+'5'
console.log(x)
if happened because of the type coercion
but if we multiply string and number the string will converted into the number
and get multiplied
ex-
let y;
y=5*'5'
console.log(y)
o/p
25
It happens because multiplication of two string does not make any sense.
x=5+null
console.log(x)
o/p
5
x=5+true
console.log(x)
o/p
6
Strings
Ex-
Normal approach
let x
const name="mangesh"
const age=22
x="hello my name is " +name+ " and i am "+age+ " year young"
console.log(x)
literal approach
//use literals
x=`hello my name is ${name} and i am ${age} year old`
console.log(x)
Proto return the method of the string
x=s.__proto__;
console.log(x)
creating object of string
const s =new String("Hello how are you")
x=typeof s
console.log(x)
x=s.length
console.log(x)
x=s[1]
console.log(x)
to lowercase
x=s.toLowerCase()
console.log(x)
to uppercase
x=s.toUpperCase()
console.log(x)
character at given postion
x=s.charAt(0)
console.log(x)
get index of given string
x=s.indexOf('e')
console.log(x)
substring for between the given index
x=s.substring(0,4)
console.log(x)
if we pass 1 argument then it start with the given index and prints the further
values
x=s.slice(-11,-6)
console.log(x)
x=s.trim()
console.log(x)
x=s.replace('Hello','Hiiii')
console.log(x)
x=s.includes('hell')
console.log(x)
x=s.valueOf()
console.log(x)
x=s.split()
console.log(x)
Numbers
We can also create the object of number
const num=new Number(5)
console.log(typeof num)
if we fixed a number then it is converted to the string.
const num=new Number(5.456543)
console.log(typeof num)
x=num.toString()
x=num.toString().length
x=num.toFixed(3)
console.log(typeof x)
console.log(x)
Precision give the floor value
x=num.toPrecision(2)
console.log(x)
print the given number in Marathi
x=num.toLocaleString('mr-IN')
console.log(x)
o/p
५.४५७
Or in the any local language
x=Number.MAX_VALUE
console.log(x)