Giter Club home page Giter Club logo

js's Introduction

Youtube Reference : Click Here


What is JavaScript?

JavaScript was initially created to “make web pages alive”.
The programs in this language are called scripts. They can be written right in a web page’s HTML and run automatically as the page loads. Scripts are provided and executed as plain text. They don’t need special preparation or compilation to run.
In this aspect, JavaScript is very different from another language called Java.

Why is it called JavaScript?


When JavaScript was created, it initially had another name: “LiveScript”. But Java was very popular at that time, so it was decided that positioning a new language as a “younger brother” of Java would help.
But as it evolved, JavaScript became a fully independent language with its own specification called ECMAScript, and now it has no relation to Java at all.
‘Java and JavaScript have the same relation as that of car and carpet ‘- Gitman
Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.
The browser has an embedded engine sometimes called a “JavaScript virtual machine”.
Different engines have different “codenames”. For example:
  • V8 – in Chrome and Opera.
  • SpiderMonkey – in Firefox.
How do engines work?
Engines are complicated. But the basics are easy.
  1. The engine (embedded if it’s a browser) reads (“parses”) the script.
  2. Then it converts (“compiles”) the script to the machine language.
  3. And then the machine code runs, pretty fast.
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and applies optimizations to the machine code based on that knowledge. When it’s done, scripts run quite fast.

Programming In Javascript :


JavaScript in Frontend
To include and run JavaScript in your HTML file, you need to include the JavaScript File by the <script> tag or you can also write the javascript code inside the HTML file in the script tag.
  1. Include External File
<script src="path_to_js_file"></script>


2) JS code inside HTML file

<script>
	//Your JS code here<br>
</script><br>


JavaScript as a programming Language

Variables


Most of the time, a JavaScript application needs to work with information. Here are two examples:
1. An online shop – the information might include goods being sold and a shopping cart.
2. A chat application – the information might include users, messages, and much more.
Variables are used to store this information.
A variable is a “named storage” for data. To create a variable in JavaScript, use the let keyword.
The statement below creates a variable with the name “message”:
let message ;
We can also change it as many times as we want:
let message;
message = 'Hello!';
message = 'World!'; // value changed
alert(message); //alert is used to pop up an alert on the page
When the value is changed, the old data is removed from the variable:

Constants

To declare a constant (unchanging) variable, use const instead of let:
const myBirthday = '18.04.1982';

Variables declared using const are called “constants”. They cannot be reassigned. An attempt to do so would cause an error

const myBirthday = '18.04.1982';
myBirthday = '01.01.2001'; // error, can't reassign the constant!<br>


When a programmer is sure that a variable will never change, they can declare it with const to guarantee and clearly communicate that fact to everyone.

TypeOf variables

Null vs undefined
undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value. Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
In short When the variable is not initialized the typeof variable is undefined, while null is is assigned to a variable by the programmer explicitly.

A string

A string in JavaScript must be surrounded by quotes.
let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed ${str}`;
In JavaScript, there are 3 types of quotes.
Double quotes: "Hello".
Single quotes: 'Hello'.
Backticks: `Hello`.
Double and single quotes are “simple” quotes. There’s no difference between them in JavaScript.

Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a string by wrapping them in ${…}, for example:

let name = "John";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, John!
// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3


The expression inside ${…} is evaluated and the result becomes a part of the string. We can put anything in there: a variable like name or an arithmetical expression like 1 + 2 or something more complex.

Please note that this can only be done in backticks. Other quotes don’t have this embedding functionality!
alert( "the result is ${1 + 2}" ); // the result is ${1 + 2} (double quotes do nothing)

Functions in JavaScript

A function is a set of statements that take inputs, do some specific computation and produces output. Basically, a function is a set of statements that performs some tasks or does some computation and then returns the result to the user.
Syntax
function functionName(Parameter1, Parameter2, ..)<br>
{
// Function body<br>
}

Objects in JavaScript

An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where a key is a string (also called a “property name”), and value can be anything.
To understand this rather abstract definition, let us look at an example of a JavaScript Object :
let school = {
name : "AIT",
location : "Pune",
established : "1994"
}


Each of these keys is referred to as properties of the object. An object in JavaScript may also have a function as a member, in which case it will be known as a method of that object.
Let us see such an example :

let college = {
name: 'AIT', 
location : 'Pune', 
established : '1994', 
displayInfo : function(){ 
console.log(`${college.name} was established  
 in ${school.established} at ${college.location}`); 
} 
} 
school.displayInfo();    

js's People

Contributors

satya9500 avatar

Watchers

James Cloos 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.