After a long days leave I am restarting my blog. I suffered from fever and i can't do things perfectly.
I am fighting with brain fog now. Ok, thanks to nobody but me..
Because I recovered from that......
First of all what is javascript and why javascript ?
JavaScript, often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have a dedicated JavaScript engine that executes the client code. These engines are also utilized in some servers and a variety of apps. The most popular runtime system for non-browser usage is Node.
Why Javascript ?
JavaScript is used to create dynamic and interactive content on websites, enhancing user experience by enabling features like animations, form validation, and real-time updates. It is also employed in web applications, mobile app development, and server-side scripting, making it a versatile programming language.
Javascript Datatypes
*String
*Number
*Bigint
*Boolean
*Undefined
*Null
*symbol
*Object
What is an Object?
Object has some shape and behaviour.Object is nothing bt Collection of attributes.
person{
name
age
qualification
gender
weight
height
}
In this person is an object and these data are the attributes.
Interview question:
Difference between Null and Undefined?
In JavaScript, both null and undefined represent the absence of a value, but they convey different meanings and arise in different contexts.
undefined indicates that a variable has been declared but has not yet been assigned a value. It also occurs when attempting to access a non-existent property of an object or when a function does not explicitly return a value.
null represents the intentional absence of any object value. It is a primitive value that signifies an empty or unknown value that has been explicitly assigned.
In simple words from my understanding,
If the assigned thing is single then it is variable.
If the assigned thing is collective then it is a object.
In Javascript variable consists of 3:
var,let,const
const - static or unchangeable once it is assigned.
let - we can declare the variable at first and we cant declare twice but we can assign any variable
var - we can declare it twice in the program
Javascript can be written internally and externally.
without index.html It can't be executed.
<br> ----> internal js file<br> <script> </p> <p><script src="myscripts.js"> ----> external js file
log is a debugging function here..
First program:
let i = 10;
console.log(i)
output:
10
let i = true;
console.log(type of(i));
type of -->datatype identifier --> number or string or boolean..
statically type : | dynamically type:
String name = "abc" | No need of mentioning
int no = 10; | data-type
Basically statically typed programming is also called as duck-typed programming.
variable --> initialization , declaration.
let i -- > declaration of the variable
let i = 10; --> initialization of the variable
let allows the assign the memory to anything after declaration
constant:
const i = 10;
i=15;
log(i); Ans:10.
because of constant variable. It value declaration is inchangeable.
interpreter --> line by line execution.
Compiler --> whole page execution.
const i = 10;
console.log("before" + i);
i=15;
console.log("after" + i);
output:
before10
type error: constant variable can't be assigned
History of javascript
JavaScript was created by Brendan Eich at Netscape in 1995, initially named Mocha, then LiveScript, and finally, JavaScript. It was designed to add dynamic and interactive elements to web pages, complementing the static nature of HTML and CSS. Its initial implementation was in Netscape Navigator, and it has since become the dominant scripting language for the web, standardized as ECMAScript.
Variables and Datatypes..
--> Dynamic type datascript.
let i = 10;
console.log(i+"5")
It automatically recognises that it is a number datatype..
ex: "pushpa" + 2
If we give commands like "abc" - 5;
it shows NaN which means not a number
increment operator and decrement operator
variable++
variable--
is the syntax for the increment and decrement operator.
It also consists of two :
pre-increment (++no)
post - increment (no++)
Difference between Var and let
- Scoping: var (Function-scoped or Global-scoped): Variables declared with var are scoped to the nearest function or, if declared outside any function, to the global scope. This means they are accessible throughout the entire function or globally, regardless of block boundaries (like if statements or for loops).
let (Block-scoped):
Variables declared with let are block-scoped, meaning they are only accessible within the specific block (defined by curly braces {}) where they are declared. This provides more granular control over variable visibility and helps prevent unintended side effects.
- Hoisting: var: var declarations are hoisted to the top of their scope (function or global) and initialized with undefined. This means you can reference a var variable before its declaration in the code without a ReferenceError, though its value will be undefined until the declaration line is reached.
let:
let declarations are also hoisted, but they are not initialized with a default value. Attempting to access a let variable before its declaration results in a ReferenceError due to the "Temporal Dead Zone."
- Re-declaration: var: var allows for re-declaration of variables within the same scope without error. This can lead to accidental overwriting of variables, making code harder to debug.
let:
let does not allow re-declaration of variables within the same block scope. Attempting to re-declare a let variable will result in a SyntaxError. This helps enforce stricter variable management and reduce potential bugs.
In summary:
Use let (and const when a variable's value should not be reassigned) in modern JavaScript development to leverage block-scoping and prevent common pitfalls associated with var, leading to more predictable and robust code.
for example :
let k = 5;
console.log(k++ - --k + k++ - ++k + k--);
console.log(k)
output:
5
6
in first it displays 5 and stores 6 in the memory. Then due to pre decrement we have to set that into 5 again. then again it display 5 in third operator 5 and stores 6 again. then by increment in 4th operator. it shows 7 in the 4th operator and as the 6th operator has decrement it shows 7 only but it stores as 6.
Avalothaan pa ithudan vilaiyattu seithigal mudivadaikirathu......
Top comments (0)