JS or JavaScript is a scripting language completely different from java. Js is used to add functionality or logic in html.
Below is the first thing you have to know which is console. Simply right click in your browser and click inspect. Here you can see the console. It is used to debug or test purpose in browser for js.
Below is the code snippet of different types of console log.
console.time('Your code took: ')
console.log('hello console');
console.log(4*4);
console.log(true);
console.log([34,67,43]);
console.log({himanshu:'this', marks: 34});
console.table({himanshu: 'this', marks:34});
console.warn('This is warning');
//console.clear();
console.timeEnd('Your code took: ');
console.assert(566<189, 'Age >189 does not exist');
console.error('This is an error');
As you can see there are different type of consoles.
- console.log(value) will print the value as it is.
- console.table(value) will print the value in table format.
- console.warn() will raise a warning massage in console tab.
- console.time(identifier variable) & console.timeEnd(same identifier variable as in "console.time()") will return the time taken to execute the block of code within these two perameters.
- console.assert(value) will give a assertion(similar to warning) if condition not met.
- console.error() will give error massage.
Ok......So, I hope everything above should be as clear as water to you guys. Lets jump into next topic which is variables.
Variables are basically containers which hold the different values like numbers, letters or words. For example in maths when we solve algebra there is a most common problem we all might've faced "Find x" (don't worry you won't get your ex back...sheesh pj :) ). Anyway there are variables in algebra like x and y with values like x=4 or x=5y something like that. So basically variables can store different types of values. I am bored now. Lets talk about real deal.
Moving forth there are three types of variables as far as I know till now in js.
- var
- let
- const
//Variable in js
//var, let, const
var name = 'Himanshu';
var marks = 3456;
var channel;
console.log(name, channel, marks);
//Rules for creating Javascript variables
/*
1. Cannot start with numbers
2. Cannot start with letter, numbers, _ or $(can start with _ or $ but these use as identifires in many frameworks
which will give error eventually)
3. Are case sensitive
*/
var $example ='example'
console.log($example)
const ownerName = 'Himanshu';
owerName = "Hari"
console.log(ownerName);
//const is a constant variable. is immutable
// cant in left undefined
//let has block level scope
var city = 'Delhi'; // global scope
console.log(city);
{
let city = 'Dubai';
console.log(city);
}
console.log(city);
const arr1 = [12,34,23,54];
arr1.push(45);//in const variable array can push the value but can't redefine
console.log(arr1);
/* Most common programming case types:
1. camelCase
2. kabab-case
3. snake_case
4. PascleCase
*/
First lets talk about var.
Var is a global variable in js which can be accessed from anywhere in the code. But most developers try not to use var due to it's nature. Suppose we have 1000 lines of code and we used var with same name. It will raise a exception which might take us an eternity to identify.
In a nutshell var can be redefined means we can change the value of var.
Let......
Let variable has block level scope and {..this is scope..} all the things written within curly braces{} is in scope. We can redefine the let value out of the scope but it will give different value then the value which is defined in scope.
Const......
Const variable is immutable by nature means once defined can't be changed through out the the code. If we need a value to be static we use const. Like fields in a column or sirname etc.
Bonus tip: We can declare global values in a block by using window object.
for example
{ window.value = 100;}
alert(window.value).
What are the restrictions while initializing a vaiable?
We can use _ or $ to initialize a variable in js but it is recommended not use these because when we work with different libraries it may cause bugs.
Also we can't use * or number(1, 2, 3.....) to initialize the value.
Sorry for the long post. Here's a potato.
Top comments (1)
You explained everything so clearly 🔥!
Keep learning ✌🏻