Java Script
Java Script
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")
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
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
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
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)
// 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'
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)
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)
to lowercase
x=s.toLowerCase()
console.log(x)
to uppercase
x=s.toUpperCase()
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)
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)
x=Number.MAX_VALUE
console.log(x)