Fundamentals - Part 1 Flashcards
1 - Shortcut to Opening Console in Chrome?
2 - Shortcut for increasing font size in chrome console
1 - ctrl + shift + j
2 - ctrl +
alert “Hello world” in chrome
alert(“hello world!”)
What is JAVASCRIPT?
JAVASCRIPT IS A HIGH-LEVEL, OBJECT-ORIENTED, MULTI-PARADIGM PROGRAMMING LANGUAGE.
What syntax we use to display (alert) the js output in chrome not in console
alert
How to display output data in console?
console.log
What is a value in js? -
◉ ‘aman’ is a value or variable ?
◉ 23 is a value or variable ?
◉ how to display ‘aman’ in console?
◉ Smallest unit of the information is known as value
◉ ‘aman’ is a value
◉ 23 is a value
◉ to show the same in consol we need to do the following
console.log(“aman”);
console.log(23);
◉ What is a variable?
◉ Syntax to assiagn a variable?
◉ What are the variable Name Convension for multiple words variable eg First Name?
◉ Is it correct to assign a variable with First Captial Letter ? Why?
◉ What type of Value are store in all capital letter variable like PI?
◉ How to write a descriptive variable naming?
◉ How to comment single line code
◉ How to comment multi line code
◉ variable is a box which store the value which we can use multiple times in a program
◉ to assign a variable in we use - let
eg.
let firstName = “aman”;
console.log(firstName);
◉ Name Variable with Multiple Words:- let firstNamePerson (Here 1st charater of 2nd and 3rd word must be Captial this type of naming is called camelcase )
or we can use underscore _
let first_name_person = “ravi”;
or $ sign can be used
let first$name$person = “aman”;
so these are the standard variable naming convensions
a variable name cannot start with a number like let 3first = "aman"
- also we can not name a variable with reserved js keyword
eg.
let new=”aman”; (this is wrong because ‘new’ is a reserved js keyword )
- ‘name’ is also a reserved keyword but we can use it. keeping in mind that this can create an error
- never start a variable name with Capital letters eg. let FirstName = "aman" (So here F is capital which is not illegal but not right way to do that)
because first charater uppercase type variable is reserved for the specific use case in js for eg. Object oriented programming
also note that variable in all uppercase like PI=”3.14” is reserved for the constant that we know will never change
Use following standard convension let firstName = "aman"; let first_name_person = "ravi"; let first$name$person = "firdosh";
Note: your variable name must be description which tell us what value it is holding eg.
-case1
let myFirstJob = 'programmer'; let myCurrentJob = 'teacher';
Case2
let job1 = 'programmer'; let job2 = 'teacher';
so we can see that case1 is more descriptve than case2
For Single Line Comment a code we need to use //
keyboard shortcut
Ctrl + /
uncomment again ctrl + /
For multiline comment
/*
Comments
*/
◉ How many Data Types are there? Name them
◉ How many types of Primitive Data types are there
2 Types:-
◉ Objects - let me = { name: 'jonas' };
◉ Everything else - Primitive let firstName = 'jonas'; let age= 30;
Now there are 7 Types of Primitive Data Types
1 - Numbers: Floating Numbers used for decimals and integers let age = 30;
2- String: Sequence of characters -> Used for text let firstName = 'Jonas';
3. Boolean: Logical type that can only be true or false � Used for taking decisions let fullAge = true;
4 - Undefined: Value taken by a variable that is not yet defined (‘empty value) eg.
let children;
5- Null: Also means ‘empty value
6- ES2015): Value that is unique and cannot be changed [Not useful for now]
7 - BigInt (ES2020): Larger integers than the Number type can hold
Note : JavaScript has dynamic typing: We do not have to manually define the data type of
the value stored in a variable. Instead, data types are determined automatically
we can use 'typeof' operator to check the variable type let javaScriptIsFun = true; console.log(javaScriptIsFun); console.log(typeof true); console.log(typeof javaScriptIsFun); console.log(typeof 'aman'); console.log(typeof 23);
// undefined variable
let year;
console. log(year);
console. log(typeof year);
year = 1991;
console.log(typeof year);
◉ What are the Difference between let, const, var variable ?
// let, const and var difference
let age = 30; age = 31;
// value of let variable can be changed later in a program
// but the value of const can not be changed once assigned
const birthYear = 1991;
/* so now if we want to change value of birthYear then it will show an error birthYear = 1992; const birthYear; this is not possible to declare an empty variable with const
Now question is which variable we should to declared
so usualy we should declare const variable if we know our variable need not to be changed in future
var is an old way of declaring variable which we will avoid now . It is pretty same as let
also we need to keep in mind the we can declare a variable without let, const or var but this is a terrible idea so we should not do that
firstName = “Aman”;
we must not do like this
*/
◉ What are the Basic Operators? Name them
//Math Operator - eg. calulate age
const ageAman = 2022 - 1992; console.log(ageAman);
const ageMushtaq = 2022 - 1965; console.log(ageMushtaq);
// To Show mutiple output with one console.log
console.log(ageAman, ageMushtaq);
// we have repeated 2022 twice so better to put the same in a variable
const currentYear = 2022; const ageMahfooz = currentYear - 1970; const ageNeha = currentYear - 1996; console.log(ageMahfooz, ageNeha);
//other arithmetic operations
console.log(ageMahfooz * 2, ageNeha / 2, 2 ** 3);
// here 2 ** 3 means 2 to the power of 3 = 2 * 2 * 3
// to concatinate two variables we can use +
const firstName = 'aman'; const lastName = 'khan';
console.log(firstName + ‘ ‘ + lastName);
// assisnment operators
let x = 10 + 5; // x = 15
x += 10 // x = x +10 = 25 becuase in line 33 (last step) value of x become 15
console.log(x);
x *= 4 // x = x * 4 = 100 becuase in line 35 (lat step) value of x become 25
console.log(x);
x++ // x = x + 1 = 101
console.log(x);
x– // x = x-1 = 100
x– // x= x-1 = 99
console.log(x);
// comparasion operators // it produce boolen values. lets check an example
console. log(ageMushtaq > ageAman); // result will be true
console. log(ageAman <= 18);// result should be false
◉ operator precedence - which operator work first ?
maths operator excute before the comparisn operator
it also follow the bodmas rule from school
◉ Strings & template literals
with template literals, we can write a string in a more normal way, and then basically insert the variables directly into the string
and then they will simply be replaced. So basically a template literal can assemble multiple pieces into one final string.
// without template literals
const firstName = "Aman"; const job = "Digital Marketer"; const birthYear = 1992; const currentYear = 2022;
const aman = “I’m “ + firstName + “, a “ + (currentYear - birthYear) + “ Years old “ + job;
console.log(aman);
// with template literals // ` backticks will be find above tab const amanNew = `I'm ${firstName}, a ${currentYear - birthYear} Years old ${job}`; console.log(amanNew);
// we can write all string without variables also
console.log(This is just all string test
);
// multiline strings
console.log(‘string with \n\
multiple \n\
lines’);
// multiline strings easy way using ` backticks
console.log(`this is an easy way to do that`);
◉ Taking decision with if else statements
◉ What is syntax of if else statement?
const firstName = "Aman"; const age = 23; const isOldEnough = age >= 18;
if (isOldEnough) { const eligible = `${firstName} is eligible for driving license 🚗`;
console.log(eligible); } else { const nA = `${firstName} has ${18 - age} years left to be eligible for a driving license ❌`; console.log(nA); }
// emoji on window 10 = window key + .
/* if else control structure
if()
{
}
else
{
}
*/
// program for 20th centry or 21st century
const birthYear = 2002; let century;
if (birthYear <= 2000) {
century = “20th Century”;
}
else { century = “21st Century” }
console.log(century);
◉ What is difference between conversion & coercion?
◉ // converting Data Type from String to number and others /* So type conversion is when we manually convert from one type to another.
One the other hand, type coercion is when JavaScript automatically converts types behind the scenes for us.
*/
◉ // Type Conversion
const inputYear = ‘1991’; // here 1991 is a string as this is in quotes
// so now if we want to do some calculate with inputYear then it will not work, let’s try
console.log(inputYear + 18); // if you are expecting result to be 2009 then you are wrong as here result will be concatenation 199118
// convert string to number console.log(Number(inputYear), inputYear); // here "N" will be upper case. So here first one is a number and second one is a string. So here we can notice that the original value of varibale has not change . Its still a string
console.log(Number(inputYear) + 18); // now result will be 2009
// we can not convert a something to number Which is imposible to convert.. example
const myName = 'Aman'; console.log(Number(myName));
// Above output will be NaN stands for Not a Number
// Now we will convert Number to string
console.log(String(23)); // here S will be upper case
// js can convert only to 2-3 types like number, string, boolen
// so this is a manual conversion but js do the same automatically which is called Coercion
◉ // type coercion
console. log(‘I am ‘ + 23 + ‘ years old’); // here coercion happens as 23 has been converted so string automatically
console. log(‘23’ - ‘10’ - 3); // this time result will be 10 which means js will convert 23 string to number . Please remeber is something is in blue then its number and if something is in yellow then it a string . here - minus operator behaves oppositly
console. log(‘23’ + ‘10’ + 3); // here output will be 23103 which means 23 is a string 10 is a string a 3 has been converted to string and all concetenate to give us 23103 result
// check for multiply and divide
console.log(‘23’ * ‘10’); // result will be 230 which means here 23 and 10 has been converted to numbers
let n = '1' + 1; // '11' n = n - 1; //10 console.log(n); //result wil be 10
// + convert number to string // - * / converted string to number
console. log(2 + 3 + 4 + ‘5’); // = 95 .. as 2+3+5 = 9+ ‘5’ = 95
console. log(‘10’ - ‘4’ - ‘3’ - 2 + ‘5’); // ‘15’ string ‘10’-‘4’ = 6 -‘3’ =3-2 =1 +’5’ = ‘15’
◉ What Falsy Value & Truthy Value?
◉ Tell 5 Falsy Value
◉ 5 falsy values: 0, empty string ‘’, undefined, null, NaN
◉ Everything else are the truthy value