Giter Club home page Giter Club logo

javascript-notes's Introduction

JavaScript-Notes

Output In Javascript:

console.log()

Commenting:

//Single line comment

/*
Multi
Line
Comment
*/

Primitive Data Types:

  • Number - let age = 22;
  • String - let firstName = "Ayan";
  • Boolean - let adult = true;
  • Undefined - Initiated but no value: let hello;
  • Null - 'empty value'
  • Symbol (ES2015)
  • Bigint (ES2020) - Integers that are too big!

Undefined Variables

When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means "Not a Number". If you concatenate a string with an undefined variable, you will get a string of undefined.

JavaScript Variables

  • Var - Re-declare it and Update the value.
  • Let - Only Update the value.
  • Const - We cannont redeclare or update the value.

Template Literal

let username = "Ayan";
let age = 22;

console.log(`Welcome ${username}, your age is ${age}.`);

Output - Welcome Ayan, your age is 22.

To Check Data Type -

let age = 22;

console.log(typeof age);

Output - Number

Type Conversion

  • String to Number - Number(x);
  • Number to String - String(a);
  • Boolean - Boolean(a);

Nullish Coalescing

It gives the output when the value is Undefined or NULL.

let age = null;

console.log(age ?? "Hi");

Output - Hi

Error Handling

try{
    some lines of code....
 }
catch(error){
    console.log(error);
    console.log(error.name);
    console.log(error.message);
    console.log(error.stack);
    console.log("something else");

Function

function greet(){
    console.log("Welcome!");
}
greet();

Welcome!

function greet(name){
    console.log(`Welcome, ${name}!`);
}
greet("Ayan");

Welcome, Ayan!

Anonymous Function

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript. However, for anonymous functions in JavaScript, we use only the function keyword without the function name.

An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. An anonymous function can also have multiple arguments, but only one expression.

The syntax below illustrates the declaration of anonymous function using normal declaration:

function() {
    // Function Body
 }

We may also declare anonymous function using arrow function technique which is shown below:

( () => {
    // Function Body...
} )();
var greet = function () {
    console.log("Welcome!");
};
 
greet();

Welcome!

Arrow Function

How to create arrow function: To write the arrow function, simply create any variable it can be const, let, or var but always do prefer with const to avoid unnecessary problems. And then assign the function code to the variable it. So from now, you can call that function by writing the parenthesis in front of that variable! With arrow function syntax, we consider function as an object and assign the definition to some variable. Following are the syntax of the arrow function:

const myFunction = (param1, param2,
    .... paramN) => { // function code }
const myFunction = (param) => { // function code }
                      or 
const myFunction = param => { // function code }  
const myFunction = param => { return param*param }
                      or 
const myFunction = param => param*param

We can omit the {} parenthesis when there is only one statement and the JavaScript considers that statement as return value, also there is no need to write parenthesis () when there is only one parameter. The arrow function cannot contain the line break between the (params) and the arrow =>, Also there should not be any space between the = and > characters.

const calculateSum = (x,y) => x + y;

console.log(calculateSum(2,3));

5

javascript-notes's People

Contributors

rooghz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.