[Notes] - Javascript from beginner to professional

 


console.log("Hello world!");


Adding JavaScript to a web page

There are two ways to link JavaScript to a web page.The first way is to type the JavaScript directly in the HTML between two <script> tags.

<html>
<script type="text/javascript">
alert("Hi there!");
</script>
</html>

Semicolons
After every statement, you should insert a semicolon. JavaScript is very forgiving and will understand many situations in which you have forgotten one, but develop

the habit of adding one after every line of code early. When you declare a code block, such as an if statement or loop, you should not end with a semicolon. It is only for the separate statements.

console.log(" Welcome");
//single line comment
/* double line comment */
let a = 10; //set value as 10
console.log("Value of a: " + a);
alert("Hi! How are you?"); <--TO display popup alert
prompt("Hi! How are you?"); <--TO take input from user
Math.random();
console.log(Math.floor(Math.random() * 100)); <--rounding random number

Declaring variables
The first time you create a variable, you declare it. And you need a special word for that: let, var, or const.The second time you call a variable, you only use the name of the existing variable to assign it a new value:

let firstname = "Uppala";
firstname = "Kishore";

let, var, and const

let and var are both used for variables that might have a new value assigned tothem somewhere in the program.var has global scope and let has block scope.Remember, a block of code will always start with { and end with }, which is how you can recognize them.

On the other hand, const is used for variables that only get a value assigned once.If you try reassigning a value declared with const, you will get an error.

The main difference between single quotes and double quotes is that you can use single quotes as literal characters in double-quoted strings, and vice versa. If you declare a string with single quotes, the string will end as soon as a second quote is detected, even if it's in the middle of a word.
Escape characters
let str = "Hello, what's your name? Is it \"Kishore\"?";
console.log(str);
let str2 = 'Hello, what\'s your name? Is it "Kishore"?';
console.log(str2);
          let str5 = "I'm containing a backslash: \\!";
Number
let intNr = 2;
let decNr = 3.5;
let expNr = 2.4e15;
But in some special cases, you will need an even bigger number.
BigInt - A BigInt data type can be recognized by the postfix n:

let bigNr = 90071992547409920n;

Symbol
Symbol is a brand new data type introduced in ES6.Symbol can be used when it is
important that variables are not equal, even though their value and type are the same.

let str1 = "JavaScript is fun!";
let str2 = "JavaScript is fun!";
console.log("These two strings are the same:", str1 === str2);

let sym1 = Symbol("JavaScript is fun!");
let sym2 = Symbol("JavaScript is fun!");
console.log("These two Symbols are the same:", sym1 === sym2);

And the output:
These two strings are the same: true
These two Symbols are the same: false

In the second part, each symbol is unique. Therefore, although they contain the same string, they are not the same, and output false when compared.

Undefined
JavaScript is a very special language. It has a special data type for a variable that has not been assigned a value. And this data type is undefined:
let unassigned;
console.log(unassigned);

We can also purposefully assign an undefined value. It is important you know that it is possible, but it is even more important that you know that manually assigning undefined is a bad practice.Alright, this can be done, but it is recommended to not do this.This is for a number of reasons—for example, checking whether two variables are the same. If one variable is undefined, and your own variable is manually set to undefined, they will be considered equal.

null
null is a special value for saying that a variable is empty or has an unknown value. This is case sensitive.
let empty = null;
To solve the issue we encountered with setting a variable as undefined, note that if you set it to null, you will not have the same problem. This is one of the reasons it is better to assign null to a variable when you want to say it is empty and unknown at first

Analyzing and modifying data types

Working out the type of a variable
Especially with null and undefined, it can be hard to determine what kind of data type you are dealing with. Let's have a look at typeof. This returns the type of the variable.
testVariable = 1;
variableTypeTest1 = typeof testVariable;
variableTypeTest2 = typeof(testVariable);
console.log(variableTypeTest1);
console.log(variableTypeTest2);
As you might assume, both methods will output number.

Converting data types
The variables in JavaScript can change types. Sometimes JavaScript does this automatically.There
are built-in functions we can use to convert the data type of our variable.

There are three conversion methods: String(), Number(), and Boolean(). The first one converts a variable to type String. It pretty much takes any value, including undefined and null, and puts quotes around it.

The second one tries to convert a variable to a number. If that cannot be done logically, it will change the value into NaN (not a number). Boolean() converts a variable to a Boolean. This will be true for 
everything except for null, undefined, 0 (number), an empty string, and NaN.

Practice2.2:
const name = "Kishore";
const age = 33;
var bool = "true";
console.log("Hello, my name is " +name+", I am "+age+" years old and I can code JavaScript\:" +bool+".")

Modulus
let nr1 = 10;
let nr2 = 3;
let result1 = nr1 % nr2;
console.log(`${nr1} % ${nr2} = ${result1}`);

Unary operators: increment and decrement
let nr1 = 4;
nr1++;
console.log(nr1);

let nr2 = 4;
nr2--;
console.log(nr2);

Prefix and postfix operators
We can have the increment operator after the operand (x++), in which case we call this the postfix unary operator. We can also have it before (++x), which is the prefix unary operator.

The postfix gets executed after sending the variable through, and then after that, the operation gets executed. 

let nr = 2;
console.log(nr++);    (output: 2)
console.log(nr);      (output: 3)

The prefix gets executed before sending the variable through, and often this is the one you will need.

let nr = 2;
console.log(++nr);    (output : 3)

Combining the operators
You can group using ( and ). The operations between the parentheses have the highest precedence. After that, the order of the operations takes place based on the type of operation (highest precedence first) and if they are of equal precedence, they take place from left to right:




Post a Comment

Previous Post Next Post