Learn javaScript Basics- Day 4

  1. HTML: html used for describing content
  2. CSS: css used for describing presentation of the content
  3. JavaScript: javascript used for describing behaviours
Run JavaScript in VS Code
  • node filename.js
or, 
  • click run button in VS code
  • create a launch.json file
  • select node.js preview
html and css are not programming language. JavaScript is a programming language. 
Programming is a set of instruction (which is written by programmer) that telling a computer what to do.  

 What does the alert() function do?

  • The alert function opens up a pop-up box with whatever text you pass to it.
What does prompt() funtion do?
  • The prompt function opens up a pop up box with a text field that allows the user to type an answer, which it will then return to code.
Primitive values are hardcoded and therefore cannot be changed. if a=10, you can change the value of x you can not change 10

Javascript data types
  • Numbers
  • String
  • booleans
  • null
  • undefined
  • object
  • function

  1. Numbers: JavaScript only has one type for numbers: 64 bit floating point values. It includes  two additional values: infinity and NaN (not a number)
  2. String:  Strings are used to represent text in JavaScript. To create a string in JavaScript, you enclose characters with either single or double quote.
  3. Booleans: Booleans data types have two values: true and false. 
  4. Object: In JavaScript, objects are king. If you understand objects, you understand JavaScript -w3schools. 
  5. null: null is a primitive values and is treated as falsy for boolean operations.
  6. undefined: undefined means a variable declared, but no value has been assigned
  7. function
typeof is a built in function that help us to see the data type of any value. 

  • typeof(15) 
  • typeof "fruits"
  • typeof prompt
  • typeof 20/"fruits"
Interview Question: 
1. What are the seven data types in javascript?
Numbers, string, boolean, null, object, function, undefined
2. What is the difference between null and undefined? 
undefined means a variable declared, but no value has been assigned. null is a primitive values and is treated as falsy for boolean operations.

3. Enter an expression in the console that evaluates to NaN. 

parseInt("Hello");


Numbers, Arithmetic Operators, Comparators

Numbers: number is one type of data type in javascript. number needs to perform arithmetic operations. 

Arthmectic Operators:  Arithmetic operators are things like: +, -, *, and /. They're used to compute values.  There are five arithmetic operators in JavaScript,

  • +
  • -
  • *
  • /
  • %
Comperators: Comparators are used to compare values and they return either true or false. 
  • > (greater than)
  • < (less than)
  • ==
  • ===
  • !=
  • !==
  • >= greater or equal
  • <= less or equal
Operator precedence: This tells code the order in which operations should be performed fast. 
  
Interview Question
1. How modulas operator perform? 
if a = 35%10, then a is store 5 which which is the remainder after dividing 35 by 10.
2. What is the order of arithmetic operations in JavaScript? 
PEMDES - parenthesis(bracket), exponent(3**4 = 81), Multiplication, Division, Addition, Subtraction


String 

String is collection of charecter that is used to reperesent a text. Use "" to enclose the text. Example: myName="Mojnu" 

Length Property

var charecterCount = "90 Day Challenge for Get a Job";
console.log(charecterCount.length);

String Concatenation

  • String concatenation is combining two or more strings into a single string. For example: 
var addTwoWord = "I Love" + "My Mother";
console.log(addTwoWord)

String Comparisons 

var checkTwoStingEqual = "I Love" !== "My Mother";
console.log(checkTwoStingEqual)

Interveiw Question
1. What is string concatenation? 
2. What happen when Concatenate a string + number? 
var stringPlusNumber = 90 + " Day";
console.log(stringPlusNumber)

3. Escape charecter? 

  • \t tab 
  • \n new line 
  • \r carriage return
  • \' start /end of string
  • \\ backslash
  • \uHHHH
Variables
  • A variable is a named reference to a location in memory for storing data.
  • javascript is untyped, it means that you don't need to declare it's type.
  • to create a variable you can simply type => var varName = value;
  • variable name can not begin with number. Ex: 90day is not correct to declare a variable
  • can not use special charecter in variable name without $ and -
  • can not have space in variable name
  • follow camel case when you declare a variable
Interview Question:
1. Declare a variable?
var daysRemain = 86;

2. Find the length?

var quote = "My name is Mojnu. I am a full stack web developer. I challenge myself in september 1 to get a job in 90 day" console.log(quote.length);

3. Give an Example of Increment or decrement?

 var days = 90; days++;  

Conditional Statements 

var days= 4;

if(days == 90) {

console.log("The End Game");

}

else if(days == 0) {

console.log("The Game Start");

}

else {

console.log("It's day 4, try hard mama");

 

Truthy or falsy condition
  • if("hello") - true condition
  • if("") - false condition
  • if(false) - falsy
  • if(0) - falsy
  • if(null) - falsy
  • if(undefined) - falsy
  • if(NaN) - falsy
Loops in Programming Language 
  • Loops can execute a block of code a number of times until complete the process
For Loop: 
for (var i = 1; i < 11; i++) {

   console.log(i);

}

//output: 1 2 3 4 5 6 7 8 9 10

While Loop: 

var days = 10;
while (days > 4) {

   console.log(days);

days--;

}

//output: 10 9 8 7 6 5


Array
  • JavaScript arrays are used to store multiple values in a single variable. -w3Schools 
  • const cars = ["Saab""Volvo""BMW"]; //declare an Array
  • To access the items of an array, an indexer must be used: console.log(cars[2]) //result: BMW
  • modify the tems in an array like this: cars[2] = "Toyota";
concat: join one or more arrays. See the example below,
    const addCars = ["Ford Motor", "Daimler", "Volkswagen"];
    const totalCars = cars.concat(addCars);
    //result totalCars = ["Saab", "Volvo", "BMW", "Ford Motor", "Daimler", "Volkswagen"]

indexOf: var flowers = ["rose" , "lily", "tulip"];
console.log(flowers.indexOf("lily"));
//result: 1
console.log(flowers.indexOf("bely"));
//result: -1 because bely flower is not in flowers array.
  • indexOf return -1 if the items not found in the array
lastIndexOf
  • lastIndexOf searches the item from the last in the array. Ex: lastIndexOf("tulip"); //output: 0
pop: 
  • remove first item from array
  • Example: https://github.com/coderMojnu/90DaysChallenge/blob/master/Day5/arrayAndFunction-day5/shift.js
unshift:

Comments

Popular posts from this blog

Journey start to become a web developer and Learn HTML - day 1 | 90 Day Code

Learn Git and host your website- Day 3