Skip to main content

Introduction of Java Script

Cheatsheets / Introduction to
JavaScript
Introduction
JavaScript
JavaScript is a programming language that powers the dynamic behavior on most
websites. Alongside HTML and CSS, it is a core technology that makes the web run.
Console.log()
The console.log() method is used to log or print messages to the console. It can also
be used to print objects and other info.
Strings
Strings are a primitive data type. They are any grouping of characters (letters, spaces,
numbers, or symbols) surrounded by single quotes ' or double quotes " .
Numbers
Numbers are a primitive data type. They include the set of all integers and floating point
numbers.
ccoonnssoollee..lloogg((''HHii tthheerree!!''));;
//// PPrriinnttss:: HHii tthheerree!!
lleett ssiinnggllee == ''WWhheerreess mmyy bbaannddiitt hhaatt??'';;
lleett ddoouubbllee == ""WWhheerreess mmyy bbaannddiitt hhaatt??"";;
lleett aammoouunntt == 66;;
lleett pprriiccee == 44..9999;;
Booleans
Booleans are a primitive data type. They can be either true or false .
Null
Null is a primitive data type. It represents the intentional absence of value. In code, it is
represented as null .
Arithmetic Operators
JavaScript supports arithmetic operators for:
lleett llaatteeTTooWWoorrkk == ttrruuee;;
lleett xx == nnuullll;;
● + addition
● - subtraction
● * multiplication
● / division
● % modulo
//// AAddddiittiioonn
55 ++ 55
//// SSuubbttrraaccttiioonn
1100 -- 55
//// MMuullttiipplliiccaattiioonn
55 ** 1100
//// DDiivviissiioonn
1100 // 55
//// MMoodduulloo
1100 %% 55
String.length
The .length property of a string returns the number of characters that make up the
string.
Methods
Methods return information about an object, and are called by appending an instance
with a period . , the method name, and parentheses.
Data Instances
When a new piece of data is introduced into a JavaScript program, the program keeps
track of it in an instance of that data type. An instance is an individual case of a data
type.
Libraries
Libraries contain methods that can be called by appending the library name with a
period . , the method name, and a set of parentheses.
Math.random()
lleett mmeessssaaggee == ''ggoooodd nniittee'';;
ccoonnssoollee..lloogg((mmeessssaaggee..lleennggtthh));;
//// PPrriinnttss:: 99
ccoonnssoollee..lloogg((''hhoowwddyy''..lleennggtthh));;
//// PPrriinnttss:: 55
//// RReettuurrnnss aa nnuummbbeerr bbeettwweeeenn 00 aanndd 11
MMaatthh..rraannddoomm(());;
MMaatthh..rraannddoomm(());;
//// ☝ MMaatthh iiss tthhee lliibbrraarryy
Math.random()
The Math.random() function returns a floating-point, random number in the range from
0 (inclusive) up to but not including 1.
Math.floor()
The Math.floor() function returns the largest integer less than or equal to the given
number.
Single Line Comments
In JavaScript, single-line comments are created with two consecutive forward slashes
// .
Multi-line Comments
In JavaScript, multi-line comments are created by surrounding the lines with /* at the
beginning and */ at the end. Comments are good ways for a variety of reasons like
explaining a code block or indicating some hints, etc.
ccoonnssoollee..lloogg((MMaatthh..rraannddoomm(())));;
//// PPrriinnttss:: 00 -- 00..99
ccoonnssoollee..lloogg((MMaatthh..fflloooorr((55..9955))));;
//// PPrriinnttss:: 55
//// TThhiiss lliinnee wwiillll ddeennoottee aa ccoommmmeenntt
//**
TThhee bbeellooww ccoonnffiigguurraattiioonn mmuusstt bbee
cchhaannggeedd bbeeffoorree ddeeppllooyymmeenntt..
**//
lleett bbaasseeUUrrll == ''llooccaallhhoosstt//ttaaxxwweebbaapppp//ccoouunnttrryy'';;
const Keyword
A constant variable can be declared using the keyword const . It must have an
assignment. Any attempt of re-assigning a const variable will result in JavaScript
runtime error.
let Keyword
let creates a local variable in JavaScript & can be re-assigned. Initialization during the
declaration of a let variable is optional. A let variable will contain undefined if
nothing is assigned to it.
Undefined
undefined is a primitive JavaScript value that represents lack of defined value. Variables
that are declared but not initialized to a value will have the value undefined .
Assignment Operators
An assignment operator assigns a value to its left operand based on the value of its
ccoonnsstt nnuummbbeerrOOffCCoolluummnnss == 44;;
nnuummbbeerrOOffCCoolluummnnss == 88;;
//// TTyyppeeEErrrroorr:: AAssssiiggnnmmeenntt ttoo ccoonnssttaanntt vvaarriiaabbllee..
lleett ccoouunntt;;
ccoonnssoollee..lloogg((ccoouunntt));; //// PPrriinnttss:: uunnddeeffiinneedd
ccoouunntt == 1100;;
ccoonnssoollee..lloogg((ccoouunntt));; //// PPrriinnttss:: 1100
vvaarr aa;;
ccoonnssoollee..lloogg((aa));;
//// PPrriinnttss:: uunnddeeffiinneedd
An assignment operator assigns a value to its left operand based on the value of its
right operand. Here are some of them:
String Concatenation
In JavaScript, multiple strings can be concatenated together using the + operator. In
the example, multiple strings and variables containing string values have been
concatenated. After execution of the code block, the displayText variable will contain
the concatenated string.
String Interpolation
String interpolation is the process of evaluating string literals containing one or more
placeholders (expressions, variables, etc).
It can be performed using template literals: text ${expression} text .
+= ● addition assignment
● -= subtraction assignment
● *= multiplication assignment
● /= division assignment
lleett nnuummbbeerr == 110000;;
//// BBootthh ssttaatteemmeennttss wwiillll aadddd 1100
nnuummbbeerr == nnuummbbeerr ++ 1100;;
nnuummbbeerr ++== 1100;;
ccoonnssoollee..lloogg((nnuummbbeerr));;
//// PPrriinnttss:: 112200
lleett sseerrvviiccee == ''ccrreeddiitt ccaarrdd'';;
lleett mmoonntthh == ''MMaayy 3300tthh'';;
lleett ddiissppllaayyTTeexxtt == ''YYoouurr '' ++ sseerrvviiccee ++ '' bbiillll iiss dduuee oonn '' ++ mmoonntthh ++ ''..'';;
ccoonnssoollee..lloogg((ddiissppllaayyTTeexxtt));;
//// PPrriinnttss:: YYoouurr ccrreeddiitt ccaarrdd bbiillll iiss dduuee oonn MMaayy 3300tthh..
Template Literals
Template literals are strings that allow embedded expressions, ${expression} . While
regular strings use single ' or double " quotes, template literals use backticks
instead.
Variables
Variables are used whenever there’s a need to store a piece of data. A variable contains
data that can be used in the program elsewhere. Using variables also ensures code reusability
since it can be used to replace the same value in multiple places.
Declaring Variables
To declare a variable in JavaScript, any of these three keywords can be used along with
a variable name:
lleett aaggee == 77;;
//// SSttrriinngg ccoonnccaatteennaattiioonn
''TToommmmyy iiss '' ++ aaggee ++ '' yyeeaarrss oolldd..'';;
//// SSttrriinngg iinntteerrppoollaattiioonn
``TToommmmyy iiss $${{aaggee}} yyeeaarrss oolldd..``;;
lleett nnaammee == ""CCooddeeccaaddeemmyy"";;
ccoonnssoollee..lloogg((``HHeelllloo,, $${{nnaammee}}``));;
//// PPrriinnttss:: HHeelllloo,, CCooddeeccaaddeemmyy
ccoonnssoollee..lloogg((``BBiillllyy iiss $${{66++88}} yyeeaarrss oolldd..``))
//// PPrriinnttss:: BBiillllyy iiss 1144 yyeeaarrss oolldd..
ccoonnsstt ccuurrrreennccyy == ''$$'';;
lleett uusseerrIInnccoommee == 8855000000;;
ccoonnssoollee..lloogg((ccuurrrreennccyy ++ uusseerrIInnccoommee ++ '' iiss mmoorree tthhaann tthhee aavveerraaggee iinnccoommee..''));;
//// PPrriinnttss:: $$8855000000 iiss mmoorree tthhaann tthhee aavveerraaggee iinnccoommee..
a variable name:
Related Courses
var is used in pre-ES6 versions ● of JavaScript.
● let is the preferred way to declare a variable when it can be reassigned.
● const is the preferred way to declare a variable with a constant value.
vvaarr aaggee;;
lleett wweeiigghhtt;;
ccoonnsstt nnuummbbeerrOOffFFiinnggeerrss == 2200;;
Course
Introduction to JavaScript
Learn the JavaScript fundamentals you'll
need for front-end or back-end

Comments

Popular posts from this blog

Top -25 Questions and Answer of Computer Hardware Fundamental Series-01

                                1. The term ‘Computer’ is derived from………. a. Latin b. German c. French d. Arabic 2. Who is the inventor of “Difference Engine”? a. Allen Turing   b. Charles Babbage c. Simur Cray d. Augusta Adaming 3. Who is the father of Computer?  a. Allen Turing b. Charles Babbage c. Simur Cray d. Augusta Adaming 4. Who is the father of Computer science? a. Allen Turing b. Charles Babbage c. Simur Cray d. Augusta Adaming 5. Who is the father of personal computer? a. Edward Robert b. Allen Turing c. Charles Babbage d. None of these 6. A CPU contains a. a card reader and a printing device   b. an analytical engine and a control unit c. a control unit and an arithmetic logic unit   d. an arithmetic logic unit and a card reader 7. Which of the following controls the process of interaction between the user and the operati...

Online learn PHP

C Programming Language Series-01

C Language C Programming Language Tutorial C language  Tutorial with programming approach for beginners and professionals, helps you to understand the C language tutorial easily. Our C tutorial explains each topic with programs. The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc. C programming is considered as the base for other programming languages, that is why it is known as mother language. next → C Programming Language Tutorial C language  Tutorial with programming approach for beginners and professionals, helps you to understand the C language tutorial easily. Our C tutorial explains each topic with programs. The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc. C programming is considered as the base for other programming languages, that is why it is known as mo...