Introduction to JavaScript
JavaScript is a High-Level
, Object-Oriented
, Multi-Paradigm
Programming language
.
High-level: No need to worry about complex stuffs like type checking and memory management.
Object-Oriented: Based on objects for storing most kind of data.
Multi-Paradigm: Different styles of programming.
Programming Language: A tool that converts our code to computer understandable code.
Role of Js in Web Development
JavaScript allows developers to add dynamic and interactive effects to any webpage.we also use Js to manipulate the content and styles, load data from remote servers and entire application in the browser which we call web applications.
JavaScript works to provide functionality in webpage like as verb works in a sentence.
- It use to develop Frontend web-applications with tools like AngularJs, ReactJs and VueJs.
- It use to develop Backend web-applications with tools like NodeJs, Express.
- It use to develop Native mobile applications with tools like Ionic, React-Native and
- It also use to develop native desktop applications with tools like electron.
“Hello World” in Js
To write Hello world in Js, Open your browser’s developer tools,
To open developer tools hit:
- For mac:
Option + ⌘ + J
- For Windows:
Shift + CTRL + J
- For Linux:
Shift + CTRL + J
Then some code into console like this👇
alert("Hello World!");
you also can do more like
let js = 'amazing'
if(js==='amazing') alert('JavaSctipt is FUN!')
*please press enter after each write line
Linking of Js file with HTML
- By using
<script>
tag inside<head>
element in yourHTML
file (Internal Linking).
<script>
let js="amazing";
if(js==="amazing") alert("JavaScript is Fun!");
console.log(20+60-25+40);
// output: 95
</script>
- By creating separate script file in the project folder and add write
<script>
tag just above the body element(External Linking).
<script src="script.js"></script>
Value and variables
In JavaScript, a variable is a container that holds a value. The value of a variable can be any JavaScript data type, such as a string, number, boolean, object, or function.
To declare a variable in JavaScript, you can use the "let", "const", or "var" keyword, followed by a variable name:
let myVariable;
const myConstant = "Hello";
var myVar;
Keep in mind,In general developers like to write camelCase naming conventions during writing javascript.
Datatypes in Js
Every values can have different types depending on the type of data that we want to hold. A value either an object or a primitive value.
The 7 Primitive Datatypes
Number: Floating point numbers 👉 Used for decimals and integers
let age = 23;
String: Sequence of characters 👉 Used for text
let name = "Rajeev";
Boolean: Logical type that can only be true or false 👉 Used for taking decisions
let fullAge = true;
Undefined: Value taken by a variable that is not yet defined (‘empty value’)
let childName;
Null: Also means ‘empty value’
Symbol (ES2015): Value that is unique and cannot be changed [Not useful for now]
BigInt (ES2020): Larger integers than the Number type can hold
☝ 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.
To check type of any variable use typeof
operator
let javaScriptIsFun = true;
console. log (typeof javaScriptIsFun)
// output: boolean
javaScriptIsFun = "YES!";
console. log (typeof javaScriptIsFun)
// output: string
let year;
console.log(typeof year, year);
//output: undefined undefined
When we check type of null
console.log(typeof null);
then it shows object as output which doesn't makes any sense at all so it is an error or bug in Javascript.However, this bug is never corrected by legacy reasons but now its not an object it should shows null.
Let const and var
let
: It create a variable that we can reassign the variable value or mutate the value during the execution time.
let age=30;
console.log(age); //30
age=31;
console.log(age); //31
const
: It create a variable that we can not reassign or immutable variable.
- We can not create empty
const
variables.
const birthYear=2001;
birthYear=2003;
// Now it shows TypeError
var
: It is the old way of defining variables prior to ES6.It works pretty much same as let
but below the surface there are many difference
var job = 'programmer';
console.log(job); //programmer
job = 'designer';
console.log(job); //designer
By default, always just use
const
andlet
when you sured that the value of the variable needs to be change at some point in your code butvar
should be completely avoided.
So stay tuned with Fundamentals of JavaScript-02
Top comments (0)