JavaScript - Lesson 01
JavaScript - Lesson 01
Basic JavaScript
Variable, Data types, String Methods
Presented by:
Ahsan Ali Mansoor
4+ Years of experience / Team Lead (React JS)
Sr. Full Stack JavaScript Developer at eBridge.tech
Setup Development Environment
Browser: Google Chrome
Code editor: Visual Studio code (or any of your choice)
Visual Studio Extensions:
Bracket Pair Colorizer
Elm Emmet
ES7 React/Redeux/GraphQL/React-Native snippets
ESLint
Live Server
Prettier - Code formatter
Let’s start coding
• Console log Hello world
• Two ways to write scripts
• Internal in file
• External File: When “src” attribute found in script tag, the inside
script tag code will be skipped.
• Frameworks / libraries like React and angular are 100% based on JavaScript: you
need to master JavaScript in order to use them!
A Brief Introduction to JavaScript
Role of JavaScript in Web Development
• ES5 Came with a lot of advancement in language then ES6 and next versions
of JS called as ES Next.
Variables and Data Types
• Variables (var, let, const)
• Variable naming conventions
• In JavaScript, there are six different Primitive (Non object / simple) data
types:
Number, String, Boolean, Undefined, symbol and Null.
• Dynamic typing
Variable Mutation and Type Coercion
Comments
There are two type of comments single line (//) and multiline (/**/)
typeof Operator
typeof returns the type of passed value or variable i.e. typeof “text”
Mutation: when we use same variable but assign it a different data type value
i.e. store 28 then ‘twenty-eight’.
JavaScript String Methods
• JavaScript, methods and properties are also available to primitive values, because
JavaScript treats primitive values as objects when executing methods and properties.
• indexOf("locate") //returns the index of (the position of) the first occurrence can also use
in array. It is Case sensitive.
• indexOf("locate", 15) //Both methods accept a second parameter as the starting position
for the search:
JavaScript String Methods
• search() //method searches a string for a specified value and returns the
position of the match
The two methods are NOT equal. These are the differences:
• The search() method cannot take a second start position argument.
• The indexOf() method cannot take powerful search values (regular
expressions).
• extracts a part of a string and returns the extracted part in a new string.
• The method takes 2 parameters: the start position, and the end position
(end not included).
• slice(-12, -6) //If a parameter is negative, the position is counted from the
end of the string.
• slice(7) //If you omit the second parameter, the method will slice out the
rest of the string
• slice(-12) //counting from the end
JavaScript String Methods
substring(start, end) Method
• replace() method does not change the string it is called on. It returns a new
string.
• By default, the replace() method replaces only the first match:
• By default, the replace() method is case sensitive.
• replace(/MICROSOFT/i, "W3Schools") // To replace case insensitive, use a
regular expression with an /i flag (insensitive)
• replace(/Microsoft/g, "W3Schools") // To replace all matches, use a regular
expression with a /g flag (global match)
The concat() method can be used instead of the plus operator. These two
lines do the same:
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ", "World!");
Note: All string methods return a new string. They don't modify the original string.
Formally said: Strings are immutable: Strings cannot be changed, only replaced.
JavaScript String Methods
trim() //method removes whitespace from both sides of a string
JavaScript String Methods
Extracting String Characters
var str = "HELLO WORLD";
Console.log('sym1 === sym1: ' + (sym1 === sym1)); //sym1 === sym1: true
Console.log('sym1 === sym2: ' + (sym1 === sym2)); //sym1 === sym2: false
Console.log('sym1 === sym3: ' + (sym1 === sym3)); //sym1 === sym3: false