FrontEnd Code Examples and Questions
FrontEnd Code Examples and Questions
Questions
What is the difference between HTML and XHTML, and how do you serve
them? 4
What are the benefits of using a framework, and which frameworks have you
worked with? 11
How do you handle errors in JavaScript, and what are some common errors
that can occur? 13
How do you test your front-end code, and what tools do you use? 15
What is the difference between null and undefined in JavaScript, and when
would you use them? 17
How do you use CSS to create a responsive layout, and what are some
common techniques? 18
What is the difference between a cookie and a session, and how do you use
them? 19
How do you optimize the performance of a website, and what tools do you
use? 20
How do you use event listeners in JavaScript, and what are some common
events you might listen for? 26
How do you use AJAX to make an HTTP request in JavaScript, and what are
some best practices? 27
How do you use a framework like React to build a user interface, and what
are some key features? 28
How do you use CSS to create animations, and what are some common
techniques? 29
How do you use Git to manage version control for a web project, and what
are some common Git commands? 31
How do you use Webpack to bundle and optimize a web project, and what
are some key features? 31
How do you use a package manager like npm to install and manage
dependencies for a web project, and what are some best practices? 34
How do you use CSS to create a responsive web layout, and what are some
common layout patterns? 35
How do you use React to create reusable UI components, and what are some
best practices for component design? 36
How do you use TypeScript to add static typing to a web project, and what
are some benefits of using TypeScript? 37
How do you use a CSS preprocessor like Sass to improve maintainability and
organization of CSS code, and what are some key features of Sass? 38
How do you use browser developer tools to debug and optimize a web
project, and what are some common features of developer tools? 39
How do you optimize a website's loading speed, and what are some common
techniques for improving performance? 39
How do you use CSS animations to create dynamic user interfaces, and what
are some common animation techniques? 40
How do you use a JavaScript library like D3.js to create data visualizations,
and what are some common chart types? 43
How do you use React components and props to create a reusable user
interface, and what are some benefits of component-based architecture? 44
How do you use TypeScript to add type checking to JavaScript code, and
what are some benefits of using TypeScript? 45
How do you use WebSockets to create real-time web applications, and what
are some advantages of using WebSockets? 46
How do you use the Fetch API to make HTTP requests and handle responses
in JavaScript, and what are some common use cases for the Fetch API? 47
Laurence Svekis https://basescripts.com/
3
How do you use serverless functions to add dynamic functionality to a web
project, and what are some advantages of serverless architecture? 48
How do you use CSS Grid to create responsive layouts, and what are some
benefits of using CSS Grid? 49
How do you use Redux to manage state in a React application, and what are
some benefits of using Redux? 50
How do you use Docker to containerize a web application, and what are
some benefits of using Docker? 51
How do you use CSS animations to add visual effects to a web page, and
what are some benefits of using CSS animations? 54
How do you use Webpack to bundle a web application, and what are some
benefits of using Webpack? 55
How do you use GraphQL to retrieve data in a web application, and what are
some benefits of using GraphQL? 57
How do you use Redux to manage state in a React application, and what are
some benefits of using Redux? 58
How do you use Axios to make HTTP requests in a web application, and what
are some benefits of using Axios? 59
How do you use React Native to build a mobile application, and what are
some benefits of using React Native? 60
Code Example:
Code Example:
<head>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
Code Example:
Code Example:
.container {
display: flex;
justify-content: center;
align-items: center;
}
.item {
flex: 1;
}
Code Example:
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
}
.item {
grid-row: 1 / 2;
grid-column: 1 / 2;
}
Code Example:
Code Example:
button.addEventListener('click', () => {
paragraph.textContent = 'Button clicked!';
paragraph.style.color = 'red';
});
Code Example:
fetchData('https://jsonplaceholder.typicode.com/todos/1
', (data) => {
console.log(data);
});
Code Example:
function App() {
return (
<div>
<h1>Hello, world!</h1>
<p>This is a React app.</p>
</div>
);
}
Code Example:
git add .
git commit -m "Added new feature"
git push origin main
Code Example:
try {
// code that might throw an error
} catch (error) {
Laurence Svekis https://basescripts.com/
13
console.error(error);
}
Code Example:
.box {
width: 200px;
height: 200px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
Code Example:
Code Example:
describe('calculateTotal', () => {
it('should return the sum of two numbers', () => {
const result = calculateTotal(2, 3);
expect(result).toEqual(5);
});
});
Code Example:
Code Example:
let foo;
console.log(foo); // undefined
foo = null;
console.log(foo); // null
Code Example:
.container {
display: flex;
flex-wrap: wrap;
}
.item {
flex-basis: 50%;
}
Code Example:
// Set a cookie
document.cookie = 'name=John; expires=Fri, 31 Dec 9999
23:59:59 GMT; path=/';
// Get a cookie
const name = document.cookie.split(';').find(cookie =>
cookie.trim().startsWith('name=')).split('=')[1];
// Start a session
req.session.userId = user._id;
Code Example:
// Use a CDN
Code Example:
javascript
Copy code
const button = document.querySelector('button');
Code Example:
javascript
Copy code
const xhr = new XMLHttpRequest();
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
console.log(xhr.response);
} else {
console.log('Error!');
}
};
xhr.open('GET', 'https://api.example.com/data');
xhr.send();
Code Example:
javascript
Copy code
import React from 'react';
function App() {
return (
<div>
<h1>Hello, world!</h1>
<p>This is a React app.</p>
</div>
);
}
Code Example:
css
Copy code
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
div {
animation: rotate 2s linear infinite;
Code Example:
css
Copy code
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.item {
flex-basis: 100%;
}
}
Code Example:
Code Example:
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
console.log(xhr.response);
} else {
console.log('Error!');
}
};
xhr.open('GET', 'https://api.example.com/data');
Code Example:
function App() {
return (
<div>
<h1>Hello, world!</h1>
<p>This is a React app.</p>
</div>
);
}
Code Example:
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
div {
animation: rotate 2s linear infinite;
Laurence Svekis https://basescripts.com/
29
}
Code Example:
Code Example:
git add .
git commit -m "Add new feature"
git push origin main
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
Code Example:
Code Example:
Code Example:
Code Example:
Code Example:
function Button(props) {
return (
<button onClick={props.onClick}>
{props.label}
</button>
);
}
Laurence Svekis https://basescripts.com/
36
How do you use TypeScript to add static
typing to a web project, and what are some
benefits of using TypeScript?
TypeScript is a superset of JavaScript that adds static typing and
other features to the language. Benefits of using TypeScript
include better code quality and maintainability, improved
developer productivity, and reduced runtime errors.
Code Example:
Code Example:
Code Example:
Code Example:
Code Example:
Code Example:
Code Example:
Code Example:
// Example usage
<Button type="submit" onClick={handleSubmit}>
Laurence Svekis https://basescripts.com/
44
Submit
</Button>
Code Example:
// Example usage
const sum = addNumbers(2, 3);
console.log(sum); // Output: 5
Code Example:
Code Example:
Code Example:
Code Example:
.item {
background-color: #ccc;
padding: 1rem;
}
Code Example:
// Example usage
store.dispatch({ type: 'INCREMENT' });
console.log(store.getState()); // Output: { count: 1 }
Code Example:
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]
Code Example:
useEffect(() => {
const interval = setInterval(() => {
setCount(count => count + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
Code Example:
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env',
'@babel/preset-react'],
},
},
Code Example:
Code Example:
const initialState = {
count: 0,
Laurence Svekis https://basescripts.com/
58
};
Code Example:
axios.get('https://api.example.com/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
Code Example: